我试图做的是使用Python通过Google Custom Search API下载图像。这是我第一次,也许我正在做一些平庸的错误捕捉Python的异常。通过Peer使用Python模块重置连接urllib
即使当我进入一些不是10054 Connection Peer By Peer的错误时,它也能正常工作。该代码是这样的,我刚刚取出来的无用部分:
try:
urllib.request.urlretrieve(myUrl['link'],str(count)+'.jpg')
except URLError as e:
print(e.reason)
有时候碰巧连接被对方复位和控制台显示此错误。
urllib.request.urlretrieve(myUrl['link'],str(count)+'.jpg')
File "C:\Python33\lib\urllib\request.py", line 210, in urlretrieve
block = fp.read(bs)
File "C:\Python33\lib\http\client.py", line 503, in read
return super(HTTPResponse, self).read(amt)
File "C:\Python33\lib\http\client.py", line 542, in readinto
n = self.fp.readinto(b)
File "C:\Python33\lib\socket.py", line 297, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
Press any key to continue . . .
现在我没有作出这样的URL工作很感兴趣,但我只是想我的循环继续下去,而不是停下来。我怎样才能捕捉到这个异常?
编辑:
我还注意到,有时代码正确地捕捉到该错误和正确print(e.reason)
输出[WinError 10054]
而不停止循环。这很奇怪。
你知道你可以从CMD窗口中复制文本?无需粘贴屏幕截图。 – mata
只是没有考虑复制。这是否违反一些规则?在那种情况下我肯定可以编辑。 – sparaflAsh
不,这并不违反任何规则,但将文字发布为图像确实没有意义。加上它需要更多的空间,并且是一个外部资源的链接(它可能消失或不可用......) – mata