爬虫学习(六)————异常处理URLerrors异常处理

# 异常处理都在urllib.error中进行处理

import urllib.request

import urllib.error

# 第一种异常:该网址不存在
url = "http://www.maodan.com/"
try:
response =urllib.request.urlopen(url)
# Exception是所有异常类的基类
except Exception as e:
print(e)
print("毛蛋访问完毕")

# 输出结果:
# <urlopen error [Errno 11004] getaddrinfo failed>
# 毛蛋访问完毕


# 第二种异常:404
url404 = "https://blog.csdn.net/qq_36350532/article/details/79525149"
try:
response = urllib.request.urlopen(url404)
except Exception as e:
print(e)
print("404访问完毕")

# 输出结果
# HTTP Error 404: Not Found
# 404访问完毕

猜你喜欢

转载自www.cnblogs.com/kuangkuangduangduang/p/10369888.html