URLError造成原因
1 本地网络未连接
2 服务器不存在
3 连接不到特点的服务器
4 httperror,httperror是urlerror子类
使用try,except来捕获异常
import urllib.request
import urllib.error
try:
urllib.request.urlopen('http://blog.csdn.net')#需要检测的代码
except urllib.error.URLError as e:
if hasattr(e,'code'):#判断是否有code属性
print(e.code)
if hasattr(e,'reason'):
print(e.reason)