用Python写了一个从txt中逐行读取图片链接并下载的程序,但一遇到坏链接,requests.get()就会引发崩溃
程序代码
import os
import requests
file = open("C:\\Users\\moles\\Documents\\SourceTree\\nsfw_data_scraper\\raw_data\\drawings\\urls_drawings.txt", mode='r')
dir = "C:\\Users\\moles\\Documents\\SourceTree\\nsfw_data_scraper\\raw_data\\drawings\\"
headers = {
'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
}
for line in file.readlines(): #依次读取每行
line = line.strip() #去掉每行头尾空白
try:
r=requests.get(line, headers=headers, verify=False, timeout=3)
except requests.exceptions.ConnectTimeout as error:
print("[ERROR]%s" % (line))
continue
r.raise_for_status()
path = dir + line.split('/')[-1]
f = open(path,'wb')
f.write(r.content)
f.close()
print ("%s" % (line))
file.close()
urls_drawings.txt的内容
其中前三条图片链接可以正常访问,后两条链接会导致程序超时崩溃。
http://arte-anime.com/images/...
http://akicocotte.weblike.jp/...
http://cdn.awwni.me/11wcd.jpg
http://64.media.tumblr.com/a3...
http://ak1.polyvoreimg.com/cg...
错误代码
Message=HTTPConnectionPool(host='ak1.polyvoreimg.com', port=80): Max retries exceeded with url: /cgi/img-thing/size/l/tid/44576395.jpg (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000012D2FE859A0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
Source=C:\Users\moles\source\repos\URLsDownload\URLsDownload\module1.py
StackTrace:
File "C:\Users\moles\source\repos\URLsDownload\URLsDownload\module1.py", line 14, in <module> (Current frame)
r=requests.get(line, headers=headers, verify=False, timeout=3)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的 except 类型没有捕捉到实际的异常,试试
requests.exceptions.ConnectionError
r.raise_for_status()
写在try里面