爬取猫咪图片——自动化批量下载
基于上一篇博文爬取猫咪照片进行自动化批量下载。
问题1.异常处理
解决方案:
try:
(code...)
except urllib.error.HTTPError as e:
print(e.reason, e.code, e.headers, sep = '\n')
except urllib.error.URLError as e:
print(type(e.reason))
if isinstance(e.reason, socket.timeout):
print('>>>time out')
else:
(code...)
问题2.网络响应问题造成的程序卡顿
解决方案:加入尝试次数,代码如下:
max_try_time = 5
i = 0
for tries in range(max_try_time):
try:
(code...)
except urllib.error.HTTPError as e:
print(e.reason, e.code, e.headers, sep = '\n')
except urllib.error.URLError as e:
print(type(e.reason))
if isinstance(e.reason, socket.timeout):
print('>>>time out--try %d times' % (t