用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)
阅读 2.8k
2 个回答

你的 except 类型没有捕捉到实际的异常,试试 requests.exceptions.ConnectionError

r.raise_for_status() 写在try里面

try:
    r=requests.get(line, headers=headers, verify=False, timeout=3)
    r.raise_for_status()
except Exception as error:
    print("[ERROR]%s" % (line))
    continue
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题