python-3.x - ConnectionResetError 异常 : [Errno 54] Connection reset by peer

标签 python-3.x selenium exception beautifulsoup python-requests

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

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/

相关文章:

java - WebDriver - 使用 selenium 检查忘记密码功能

android - 获取 "com.android.ddmlib.AdbCommandRejectedException: closed"异常?

debugging - Q Promises - 将拒绝转换为未处理的异常

python-3.x - 在 Python 中创建 jTDS 连接字符串

python - 在 python3 中加入字符串列表的第一个元素

python - Django: IOError [Errno 2] 使用 python 读取 JSON 文件路径时没有这样的文件或目录

python-3.x - SyntaxError;名称 'x'是参数且非本地

python - 如何记录 pyvirtualdisplay session

selenium - 而不是 sendkeys 最后从剪贴板复制的项目被输入到元素中

python - Python 2 和 3 中捕获的异常实例的范围