Python urllib2 URLError异常?
我之前在 Windows XP 计算机上安装了 Python 2.6.2 并运行以下代码:
import urllib2
import urllib
page = urllib2.Request('http://www.python.org/fish.html')
urllib2.urlopen( page )
我收到以下错误。
Traceback (most recent call last):<br>
File "C:\Python26\test3.py", line 6, in <module><br>
urllib2.urlopen( page )<br>
File "C:\Python26\lib\urllib2.py", line 124, in urlopen<br>
return _opener.open(url, data, timeout)<br>
File "C:\Python26\lib\urllib2.py", line 383, in open<br>
response = self._open(req, data)<br>
File "C:\Python26\lib\urllib2.py", line 401, in _open<br>
'_open', req)<br>
File "C:\Python26\lib\urllib2.py", line 361, in _call_chain<br>
result = func(*args)<br>
File "C:\Python26\lib\urllib2.py", line 1130, in http_open<br>
return self.do_open(httplib.HTTPConnection, req)<br>
File "C:\Python26\lib\urllib2.py", line 1105, in do_open<br>
raise URLError(err)<br>
URLError: <urlopen error [Errno 11001] getaddrinfo failed><br><br><br>
I installed Python 2.6.2 earlier on a Windows XP machine and run the following code:
import urllib2
import urllib
page = urllib2.Request('http://www.python.org/fish.html')
urllib2.urlopen( page )
I get the following error.
Traceback (most recent call last):<br>
File "C:\Python26\test3.py", line 6, in <module><br>
urllib2.urlopen( page )<br>
File "C:\Python26\lib\urllib2.py", line 124, in urlopen<br>
return _opener.open(url, data, timeout)<br>
File "C:\Python26\lib\urllib2.py", line 383, in open<br>
response = self._open(req, data)<br>
File "C:\Python26\lib\urllib2.py", line 401, in _open<br>
'_open', req)<br>
File "C:\Python26\lib\urllib2.py", line 361, in _call_chain<br>
result = func(*args)<br>
File "C:\Python26\lib\urllib2.py", line 1130, in http_open<br>
return self.do_open(httplib.HTTPConnection, req)<br>
File "C:\Python26\lib\urllib2.py", line 1105, in do_open<br>
raise URLError(err)<br>
URLError: <urlopen error [Errno 11001] getaddrinfo failed><br><br><br>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你做错了。
You're doing it wrong.
查看 urllib2 源代码中回溯指定的行:
您将看到以下片段:
因此,看起来源代码是套接字错误,而不是与 HTTP 协议相关的错误。可能的原因:您不在线,您位于限制性防火墙后面,您的 DNS 已关闭,...
所有这些都与事实无关,如 mcandre指出,你的代码是错误的。
Have a look in the urllib2 source, at the line specified by the traceback:
There you'll see the following fragment:
So, it looks like the source is a socket error, not an HTTP protocol related error. Possible reasons: you are not on line, you are behind a restrictive firewall, your DNS is down,...
All this aside from the fact, as mcandre pointed out, that your code is wrong.
名称解析错误。
getaddrinfo
用于解析请求中的主机名 (python.org
)。如果失败,则意味着无法解析该名称,因为:Name resolution error.
getaddrinfo
is used to resolve the hostname (python.org
)in your request. If it fails, it means that the name could not be resolved because:Windows Vista,python 2.6.2
是404页面,对吧?
Windows Vista, python 2.6.2
It's a 404 page, right?
DJ
首先,我认为没有理由导入 urllib;我只见过 urllib2 用于完全替换 urllib,并且我不知道 urllib 中没有有用的功能,但 urllib2 中却缺少这些功能。
接下来,我注意到 http://www.python.org/fish.html 给出了对我来说 404 错误。 (这并不能解释您所看到的回溯/异常。我得到
urllib2.HTTPError: HTTP Error 404: Not Found
通常,如果您只想默认获取网页(没有添加特殊的 HTTP 标头、执行任何类型的 POST 等),那么以下内容就足够了:
DJ
First, I see no reason to import urllib; I've only ever seen urllib2 used to replace urllib entirely and I know of no functionality that's useful from urllib and yet is missing from urllib2.
Next, I notice that http://www.python.org/fish.html gives a 404 error to me. (That doesn't explain the backtrace/exception you're seeing. I get
urllib2.HTTPError: HTTP Error 404: Not Found
Normally if you just want to do a default fetch of a web pages (without adding special HTTP headers, doing doing any sort of POST, etc) then the following suffices: