I cannot successfully build an exception for the error I am receiving:
ConnectionResetError: [Errno 54] Connection reset by peer
Here is my code:
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')
These exceptions I have tested do not work. Any suggestions on how I can successfully handle this error?
edit: text fix
It seems
urllib
is throwing the exception. In python 3 ConnectionResetError is a built in exception, just use:You don't seem to be using the
requests
module in any case - that should have been your hint.