URLError: (1)没有网络 (2)服务器连接失败 (3)找不到服务器
HTTPError: (1)连接上了,url写的有错误,得不到资源 urlError的子情况
可通过try,捕获错误信息
代码语言:javascript复制import urllib.request
import urllib.parse
import urllib.error
url = 'https://blog.csdn.net/weixin_44041700/article/details/10748437'
try:
response = urllib.request.urlopen(url)
print(response)
except urllib.error.HTTPError as e:
print(e)
except urllib.error.URLError as e:
print(e)
#常规错误可以用下面这个
try:
#...
except Exception as e:
print(e)
else:
#...