我正在尝试将jpeg保存到url列表中的文件中。此代码经常随机地超时。它已经节省了多达113个jpeg,还有很多,有时在超时之前只能节省10个。有没有办法等待,所以超时不会发生?我试过在评论部分睡觉没有运气。感谢您的反馈!
下面是超时错误消息:
import urllib.request
import urllib
import codecs
from urllib import request
import time
import csv
class File:
def __init__(self, data):
self.data = data
file = File("1")
with open("file.csv", encoding = "utf8") as f1:
file.data = list(csv.reader(f1, skipinitialspace = True))
for i in file.data[1:]:
if len(i[27]) != 0:
#i[14] creates a unique jpeg file name in the dir
image = open('C:\\aPath'+i[14]+'.JPG', 'wb')
path = 'aPath' + i[14] + '.JPG'
#time.sleep(2) Tried sleep here, didn't work
#i[27] is a working jpeg url
urllib.request.urlretrieve(i[27], path)
image.close()
print('done!')
答案 0 :(得分:1)
没有办法阻止异常。您需要捕获异常并重试。
...
for i in file.data[1:]:
if not i[27]:
continue
path = 'aPath' + i[14] + '.JPG'
while True: # retry loop
try:
urllib.request.urlretrieve(i[27], path)
break # On success, stop retry.
except TimeoutError:
print('timeout, retry in 1 second.')
time.sleep(1)
顺便说一句,如果您使用urllib.request.urlretrieve
,则无需打开文件。