python 爬虫 异常处理

import urllib.request
import urllib.error
try:
data =urllib.request.urlopen("http://blog.csdn.net").read()
print(data.decode())
except urllib.error.URLError as err:
if hasattr(err,"code"):
print(err.code)
if hasattr(err,"reason"):
print(err.reason)
posted @ 2017-11-20 21:42  沧海一粒水  阅读(149)  评论(0编辑  收藏  举报