您的位置:首页 > 其它

Why am I getting an AttributeError when trying to print out(转载)

2016-05-10 18:13 459 查看
9down
votefavorite
3

I am learning about urllib2 by following this tutorial http://docs.python.org/howto/urllib2.html#urlerrorRunning
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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: