用Python写了一个从txt中逐行读取图片链接并下载的程序,但一遇到坏链接,requests.get()就会引发崩溃

用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)


回答:

你的 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

以上是 用Python写了一个从txt中逐行读取图片链接并下载的程序,但一遇到坏链接,requests.get()就会引发崩溃 的全部内容, 来源链接: utcz.com/p/937938.html

回到顶部