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

来源:互联网 发布:jquery.easing.min.js 编辑:程序博客网 时间:2024/06/07 22:31

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 urllib2req = 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.reasonAttributeError: '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 urllib2req = urllib2.Request('http://www.pretend-o-server.org')try:    urllib2.urlopen(req)except urllib2.URLError, e:    print e.code
0 0
原创粉丝点击