【问题标题】:Exception for ConnectionResetError: [Errno 54] Connection reset by peerConnectionResetError 异常:[Errno 54] 对等方重置连接
【发布时间】:2017-04-12 15:00:30
【问题描述】:

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

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

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

编辑:文本修复

【问题讨论】:

  • 你是说except语句没有捕捉到你的错误吗?你能发布整个堆栈吗? urllib 不是请求的一部分,如果这引发了你的问题。

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


【解决方案1】:

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

except ConnectionResetError:

在任何情况下,您似乎都没有使用 requests 模块 - 这应该是您的提示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    相关资源
    最近更新 更多