我无法成功为收到的错误构建异常:
ConnectionResetError: [Errno 54] Connection reset by peer
这是我的代码:
try:
for img in soup.findAll('img', {'class': 'thisclass'}):
print('Image #:' + str(counter) + ' URL: ' + img['src'])
myurl = img['src']
urllib.request.urlretrieve(str(myurl), str(mypath)+str(foldername)+'/webp/'+str(foldername)+str(counter)+'.webp')
im = Image.open(str(mypath)+str(foldername)+'/webp/'+ str(foldername) +str(counter)+'.webp').convert("RGB")
im.save(str(mypath)+str(foldername)+'/jpg/'+ str(foldername) +str(counter)+'.jpg','jpeg')
print('Downloaded Image ' + str(counter))
counter = counter + 1
sleep(random.uniform(1.3,2.4))
except requests.exceptions.ConnectionResetError:
print('Handle Exception')
except requests.exceptions.ConnectionError:
print('Handle Exception')
我测试过的这些异常不起作用。关于如何成功处理此错误有什么建议吗?
编辑:文本修复
最佳答案
看起来urllib
正在抛出异常。在Python 3 ConnectionResetError是一个内置异常,只需使用:
except ConnectionResetError:
无论如何,您似乎都没有使用 requests
模块 - 这应该是您的提示。
关于python-3.x - ConnectionResetError 异常 : [Errno 54] Connection reset by peer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40851926/