ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

在python中使用urllib时,双引号和单引号字符串之间的行为不同

2019-11-20 18:09:46  阅读:367  来源: 互联网

标签:python-3-4 beautifulsoup urllib python


我是python的新手,我知道单引号和双引号字符串没有区别.
但是我发现了一些不同的行为.

from bs4 import BeautifulSoup
import urllib.request

url1 = "http://www.backpackers.com.tw/forum/forumdisplay.php?f=310"
url2 = 'http://www.backpackers.com.tw/forum/forumdisplay.php?f=310'

如果我运行:

response = urllib.request.urlopen(url1)

结果:脚本完成无错误

如果我跑步:

response = urllib.request.urlopen(url2)

结果:错误

C:\Users\user1\Desktop\scrape>python backpacker_tw.py
Traceback (most recent call last):
  File "C:\Python34\lib\urllib\request.py", line 1189, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "C:\Python34\lib\http\client.py", line 1090, in request
    self._send_request(method, url, body, headers)
  File "C:\Python34\lib\http\client.py", line 1128, in _send_request
    self.endheaders(body)
  File "C:\Python34\lib\http\client.py", line 1086, in endheaders
    self._send_output(message_body)
  File "C:\Python34\lib\http\client.py", line 924, in _send_output
    self.send(msg)
  File "C:\Python34\lib\http\client.py", line 859, in send
    self.connect()
  File "C:\Python34\lib\http\client.py", line 836, in connect
    self.timeout, self.source_address)
  File "C:\Python34\lib\socket.py", line 509, in create_connection
    raise err
  File "C:\Python34\lib\socket.py", line 500, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the
 target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "backpacker_tw.py", line 7, in <module>
    response = urllib.request.urlopen(url2)
  File "C:\Python34\lib\urllib\request.py", line 153, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python34\lib\urllib\request.py", line 455, in open
    response = self._open(req, data)
  File "C:\Python34\lib\urllib\request.py", line 473, in _open
    '_open', req)
  File "C:\Python34\lib\urllib\request.py", line 433, in _call_chain
    result = func(*args)
  File "C:\Python34\lib\urllib\request.py", line 1215, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Python34\lib\urllib\request.py", line 1192, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10061] No connection could be ma
de because the target machine actively refused it>

是错误还是我错过的任何东西?

C:\Users\user1\Desktop\scrape>python -V
Python 3.4.1

解决方法:

到文档! PEP 8,几乎全部都结束于python代码格式,并指出“在Python中,单引号字符串和双引号字符串是相同的.”这是由Python的创建者编写的,我将信奉他的话.

查看您的堆栈跟踪信息,我看到错误没有连接可能是错误的
de是因为目标计算机主动拒绝了它,所以也许这意味着那时服务器上发生了一些奇怪的事情?

标签:python-3-4,beautifulsoup,urllib,python
来源: https://codeday.me/bug/20191120/2045721.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有