如何在列表URL和python的帮助下下载图像,但并非所有URL都处于活动状态?
我正试图借助他们网站中提供的url从image net数据集中下载网球图像,但我的代码总是在到达不存在的url后停止执行如何在列表URL和python的帮助下下载图像,但并非所有URL都处于活动状态?,python,python-3.x,python-requests,Python,Python 3.x,Python Requests,我正试图借助他们网站中提供的url从image net数据集中下载网球图像,但我的代码总是在到达不存在的url后停止执行 import requests path = "./imageslist.txt" j = 0 file1 = open(path,'r') for i in file1.readlines(): imagename = "Image{0}.jpg".format(j) result = requests.get(i) i
import requests
path = "./imageslist.txt"
j = 0
file1 = open(path,'r')
for i in file1.readlines():
imagename = "Image{0}.jpg".format(j)
result = requests.get(i)
if result.status_code == 200:
print(i)
image = result.raw.read()
open(imagename,"wb").write(image)
j = j+1
它显示了这个错误:
ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='www.jpmorganchaseopen.com', port=80): Max retries exceeded with url: /images/tennisball.jpg%0A (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000186FDAD0A08>: Failed to establish a new connection: [Errno 11001] getaddrinfo
failed'))
ConnectionError(e,request=request)
requests.exceptions.ConnectionError:HTTPConnectionPool(host='www.jpmorganchaseopen.com',port=80):url:/images/tennisball.jpg%0A超过了最大重试次数(由NewConnectionError引起)(':未能建立新连接:[Errno 11001]getaddrinfo
失败('))
如何处理此错误?尝试以下操作:
import requests
path = "./imageslist.txt"
with open(path, 'r') as file1:
all_links = [i.strip() for i in file1.readlines()]
j = 0
imagename = f"Image{j}.jpg"
for link in all_links:
result = requests.get(link)
if result.status_code == 200:
try:
image = result.raw.read()
open(imagename, "wb").write(image)
except Exception:
pass
else:
pass
j += 1
在执行
file1.readlines()
时,需要去掉链接中存在的任何分隔符,如'\n'
。也可以使用f字符串(更快)代替.format
格式。当它到达一个死url或一个坏url时,我仍然会收到相同的错误,我希望它跳到下一个,如果它得到相同的error@HarshvardhanChandirasekar请您添加imageslist.txt
文件,或者特别是您收到错误的链接!