为什么我要打印出来时,我得到一个AttributeError [英] Why am I getting an AttributeError when trying to print out
问题描述
我按照这个教程<一个学习的urllib2 href=\"http://docs.python.org/howto/urllib2.html#urlerror\">http://docs.python.org/howto/urllib2.html#urlerror运行低于code从教程会产生不同的结果。
进口的urllib2REQ = urllib2.Request('HTTP://www.$p$ptend-o-server.org')
尝试:
urllib2.urlopen(REQ)
除了urllib2.URLError,E:
打印e.reason
Python的跨preTER吐出这回
回溯(最后最近一次调用):
文件urlerror.py,8号线,上述&lt;&模块GT;
打印e.reason
AttributeError的:HTTPError这样的'对象有没有属性理由
如何走到这是怎么回事?
更新
当我尝试打印出code属性正常工作
进口的urllib2REQ = urllib2.Request('HTTP://www.$p$ptend-o-server.org')
尝试:
urllib2.urlopen(REQ)
除了urllib2.URLError,E:
打印E,code
我得到了AttributeError错误是因为我使用OpenDNS的原因。显然,甚至当你在一个虚假的URL传递,OpenDNS的喜欢它的存在会将其。所以切换到Google的DNS服务器后,我得到预期的结果是:
[错误-2]产品名称或服务不已知
此外,我要提到我得到运行该code这是不计一切的尝试和追踪除
从urllib2的进口申请,的urlopen,URLError,HTTPError这样的REQ =请求('HTTP://www.$p$ptend_server.com')
的urlopen(REQ)
这是
回溯(最后最近一次调用):
文件urlerror.py,5号线,上述&lt;&模块GT;
的urlopen(REQ)
文件/usr/lib/python2.6/urllib2.py,线路126在的urlopen
返回_opener.open(URL,数据,超时)
文件/usr/lib/python2.6/urllib2.py,397线,开放
响应=甲基(REQ,响应)
文件/usr/lib/python2.6/urllib2.py线510,在HTTP_RESPONSE
HTTP,请求,响应,code,味精,HDRS)
文件/usr/lib/python2.6/urllib2.py,线435错误
返回self._call_chain(*参数)
文件/usr/lib/python2.6/urllib2.py,369线,在_call_chain
结果= FUNC(*参数)
文件/usr/lib/python2.6/urllib2.py,线路518,在http_error_default
引发HTTPError(req.get_full_url(),code,味精,HDRS,FP)
urllib2.HTTPError:HTTP错误404:未找到
这是一种温和的(WO)的人吗?从IRC #python告诉我是非常奇怪的,然后问我是否是使用OpenDNS的,而我回答说是的。因此,他们建议我切换到谷歌的,我继续做。
I am learning about urllib2 by following this tutorial http://docs.python.org/howto/urllib2.html#urlerror Running the code below yields a different outcome from the tutorial
import urllib2
req = urllib2.Request('http://www.pretend-o-server.org')
try:
urllib2.urlopen(req)
except urllib2.URLError, e:
print e.reason
Python interpreter spits this back
Traceback (most recent call last):
File "urlerror.py", line 8, in <module>
print e.reason
AttributeError: 'HTTPError' object has no attribute 'reason'
How come this is happening?
UPDATE
When I try to print out the code attribute it works fine
import urllib2
req = urllib2.Request('http://www.pretend-o-server.org')
try:
urllib2.urlopen(req)
except urllib2.URLError, e:
print e.code
The reason I got the AttributeError was because I was using OpenDNS. Apparently even when you pass in a bogus URL, OpenDNS treats it like it exists. So after switching to Googles DNS server, I am getting the expected result which is:
[Errno -2] Name or service not known
Also I should mention the traceback I got for running this code which is everything excluding try and except
from urllib2 import Request, urlopen, URLError, HTTPError
req = Request('http://www.pretend_server.com')
urlopen(req)
is this
Traceback (most recent call last):
File "urlerror.py", line 5, in <module>
urlopen(req)
File "/usr/lib/python2.6/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.6/urllib2.py", line 397, in open
response = meth(req, response)
File "/usr/lib/python2.6/urllib2.py", line 510, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.6/urllib2.py", line 435, in error
return self._call_chain(*args)
File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/usr/lib/python2.6/urllib2.py", line 518, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found
which a kind gentle(wo)man? from IRC #python told me was highly strange and then asked if I was using OpenDNS to which I replied yes. So they suggested I switch it to Google's which I proceeded to do.
这篇关于为什么我要打印出来时,我得到一个AttributeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!