为什么这个简单的Python代码不起作用?
import urllib
file = urllib.urlopen('http://www.google.com')
print file.read()
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
Traceback (most recent call last):
File "C:\workspace\GarchUpdate\src\Practice.py", line 26, in <module>
file = urllib.urlopen('http://www.google.com')
File "C:\Python26\lib\urllib.py", line 87, in urlopen
return opener.open(url)
File "C:\Python26\lib\urllib.py", line 206, in open
return getattr(self, name)(url)
File "C:\Python26\lib\urllib.py", line 345, in open_http
h.endheaders()
File "C:\Python26\lib\httplib.py", line 892, in endheaders
self._send_output()
File "C:\Python26\lib\httplib.py", line 764, in _send_output
self.send(msg)
File "C:\Python26\lib\httplib.py", line 723, in send
self.connect()
File "C:\Python26\lib\httplib.py", line 704, in connect
self.timeout)
File "C:\Python26\lib\socket.py", line 514, in create_connection
raise error, msg
IOError: [Errno socket error] [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Run Code Online (Sandbox Code Playgroud)
我已经尝试了几个不同的页面,但我永远无法让urlopen方法正确执行.
您的代码不是这里的问题。
您的 IE 中有代理设置吗?
urllib.urlopen 的 python 文档是这样说的:
在 Windows 环境中,如果未设置代理环境变量,
则从注册表的 Internet 设置
部分获取代理设置。