本文介绍了ConnectionResetError的异常:[Errno 54]连接由对等体重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法成功构建我收到的错误异常:

I cannot successfully build an exception for the error I am receiving:

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')

我测试过的这些异常不起作用。任何有关如何成功处理此错误的建议?

These exceptions I have tested do not work. Any suggestions on how I can successfully handle this error?

编辑:文本修复

推荐答案

似乎 urllib 正在抛出异常。在python 3 是一个内置的异常,只需使用:

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.

这篇关于ConnectionResetError的异常:[Errno 54]连接由对等体重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 03:18