Exception for ConnectionResetError: [Errno 54] Con

2019-07-29 10:09发布

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

1条回答
Bombasti
2楼-- · 2019-07-29 10:57

It seems urllib is throwing the exception. In python 3 ConnectionResetError is a built in exception, just use:

except ConnectionResetError:

You don't seem to be using the requests module in any case - that should have been your hint.

查看更多
登录 后发表回答