0

復位我不能成功地建立我收到錯誤的異常:例外爲ConnectionResetError:[錯誤54]連接由對等

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

這些例外我已經測試不起作用。有關如何成功處理此錯誤的任何建議?

編輯:文修復

+0

你是說except語句沒有發現你的錯誤?你可以發佈整個堆棧嗎? urllib不是請求的一部分,如果這是拋出錯誤,那就是你的問題。 – kabanus

回答

3

看來urllib被拋出異常。在Python 3 ConnectionResetError是一個內置的例外,只需使用:

except ConnectionResetError: 

你似乎並不被使用在任何情況下requests模塊 - 這應該是你的暗示。

相關問題