我正在尝试下载页面的HTML(http://www.google.com在这种情况下),但我收到了错误.这是我的交互式提示会话:
Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> import urllib.request
>>> html = urllib.request.urlopen("http://www.google.com")
Traceback (most recent call last):
File "\\****.****.org\myhome\python\lib\urllib\request.py", line 1136, in
do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "\\****.****.org\myhome\python\lib\http\client.py", line 964, in req
uest
self._send_request(method, url, body, headers)
File "\\****.****.org\myhome\python\lib\http\client.py", line 1002, in _s
end_request
self.endheaders(body)
File "\\****.****.org\myhome\python\lib\http\client.py", line 960, in end
headers
self._send_output(message_body)
File "\\****.****.org\myhome\python\lib\http\client.py", line 805, in _se
nd_output
self.send(msg)
File "\\****.****.org\myhome\python\lib\http\client.py", line 743, in sen
d
self.connect()
File "\\****.****.org\myhome\python\lib\http\client.py", line 721, in con
nect
self.timeout, self.source_address)
File "\\****.****.org\myhome\python\lib\socket.py", line 404, in create_c
onnection
raise err
File "\\****.****.org\myhome\python\lib\socket.py", line 395, in create_c
onnection
sock.connect(sa)
socket.error: [Errno 10061] No connection could be made because the target machi
ne actively refused it
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "\\****.****.org\myhome\python\lib\urllib\request.py", line 138, in
urlopen
return opener.open(url, data, timeout)
File "\\****.****.org\myhome\python\lib\urllib\request.py", line 369, in
open
response = self._open(req, data)
File "\\****.****.org\myhome\python\lib\urllib\request.py", line 387, in
_open
'_open', req)
File "\\****.****.org\myhome\python\lib\urllib\request.py", line 347, in
_call_chain
result = func(*args)
File "\\****.****.org\myhome\python\lib\urllib\request.py", line 1156, in
http_open
return self.do_open(http.client.HTTPConnection, req)
File "\\****.****.org\myhome\python\lib\urllib\request.py", line 1139, in
do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 10061] No connection could be made
because the target machine actively refused it>
>>>
Run Code Online (Sandbox Code Playgroud)
我最好的猜测是我的网络的防火墙阻止了连接(大多数/所有80以外的端口被阻止).不过,我不明白为什么; 在端口80上没有Python连接?有谁知道发生了什么?
小智 9
urllib从Internet Explorer获取代理设置,通常位于 Tools->Internet Options->Connections->Lan Settings
如果你有代理,请确保它在IE中是正确的,或者在使用urllib时设置它.如果您不使用代理进行浏览,请确保IE中的代理设置为空.
几个小时之后我一直在努力解决这个问题,直到我意识到我曾经安装的第三方应用程序改变了IE中的设置,而我在没有代理的情况下在Firefox中愉快地浏览.
另一个解决方案是在此链接中显示如何强制urllib忽略IE的代理设置
该错误来自操作系统级别,与Python无关。对于任何编程语言来说都是一样的。
请联系您的网络管理员以了解问题所在,除非您可以自行解决防火墙问题。如果您需要 HTTP 代理,那么您可以为 urllib 配置一个代理。