Python:URLError:

时间:2013-04-04 20:08:04

标签: python python-2.7 urllib2 urllib

操作系统:Windows 7; Python 2.7.3使用Python GUI Shell

我正在尝试通过Python阅读网站,有几位作者使用urlliburllib2库。为了将网站存储在变量中,我看到了类似的方法:

import urllib
import urllib2
g = "http://www.google.com/"
read = urllib2.urlopen(g)

最后一行在120秒后产生错误:

> Traceback (most recent call last):   File "<pyshell#27>", line 1, in
> <module>
>     r = urllib2.urlopen(o)   File "C:\Python27\lib\urllib2.py", line 126, in urlopen
>     return _opener.open(url, data, timeout)   File "C:\Python27\lib\urllib2.py", line 400, in open
>     response = self._open(req, data)   File "C:\Python27\lib\urllib2.py", line 418, in _open
>     '_open', req)   File "C:\Python27\lib\urllib2.py", line 378, in _call_chain
>     result = func(*args)   File "C:\Python27\lib\urllib2.py", line 1207, in http_open
>     return self.do_open(httplib.HTTPConnection, req)   File "C:\Python27\lib\urllib2.py", line 1177, in do_open
>     raise URLError(err) URLError: <urlopen 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>

我尝试绕过g变量并尝试urlopen("http://www.google.com/")但没有成功(它在相同的时间长度后生成相同的错误)。

3 个答案:

答案 0 :(得分:13)

错误代码10060表示无法连接到远程对等方。这可能是由于网络问题或主要是您的设置问题,例如代理设置。

您可以尝试将同一主机与其他工具(例如ncat)和/或同一本地网络中的另一台PC连接,以找出问题发生的位置。

对于代理问题,这里有一些材料:

Using an HTTP PROXY - Python

Why can't I get Python's urlopen() method to work on Windows?

希望它有所帮助!

答案 1 :(得分:1)

答案(基本进步!)

错误:10060 添加超时参数以请求解决了我的问题。

示例1

import urllib
import urllib2
g = "http://www.google.com/"
read = urllib2.urlopen(g, timeout=20)

示例2

我发出GET请求时也发生了类似的错误。同样,传递timeout参数可以解决10060错误。

response = requests.get(param_url, timeout=20)

答案 2 :(得分:0)

这是因为代理设置。 我也遇到了同样的问题,我无法使用任何从互联网上获取数据的模块。 有简单的步骤:
1。打开控制面板
2。打开互联网选项
连接标签下的 3。 打开LAN设置
4. :进入高级设置,取消标记所有内容删除其中的所有代理。或者您可以取消标记代理服务器中的复选框,这也将执行相同的操作 单击“确定” 5。 保存所有设置 你完成了 尝试再次运行程序,它必须工作 它至少对我有用