为了防止打印对象中不包含的属性时报错, 应先判断对象中是否有此属性, 有才打印
import urllib2
req = urllib2.Request('http://blog.csdn.net/cqcre')
try:
urllib2.urlopen(req)
except urllib2.URLError, e:
if hasattr(e,"code"):
print e.code
if hasattr(e,"reason"):
print e.reason
else:
print "OK"
python技术交流群: 577128300
网友评论