Dav*_* IV 6 python exception-handling
我正在构建一个Python脚本,该脚本在数据库中搜索所有URL,然后按照这些URL查找断开的链接。此脚本需要在打开链接时遇到错误时使用异常处理进行记录,但是它开始遇到一个错误,我完全无法为以下命令编写except语句:
Traceback (most recent call last):
File "exceptionerror.py", line 97, in <module>
raw_response = response.read().decode('utf8', errors='ignore')
File "/usr/lib/python3.4/http/client.py", line 512, in read
s = self._safe_read(self.length)
File "/usr/lib/python3.4/http/client.py", line 662, in _safe_read
chunk = self.fp.read(min(amt, MAXAMOUNT))
File "/usr/lib/python3.4/socket.py", line 371, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
except SocketError as inst:
brokenlinksflag = 1
brokenlinks = articlelinks[j] + ' ' + sys.exc_info()[0] + ', ' + brokenlinks
continue
Run Code Online (Sandbox Code Playgroud)
和:
except ConnectionResetError as inst:
brokenlinksflag = 1
brokenlinks = articlelinks[j] + ' ' + sys.exc_info()[0] + ', ' + brokenlinks
continue
Run Code Online (Sandbox Code Playgroud)
甚至是一个完整的通用异常,试图捕获所有错误,以免杀死整个脚本:
except:
print("This link was not caught by defined exceptions: " + articlelinks[j])
continue
Run Code Online (Sandbox Code Playgroud)
我完全不知道如何让我的脚本捕获此错误,以便它可以继续检查断开的链接而不是硬失败。它是断断续续的,所以我不认为链接断开了,而且我觉得即使我已经识别了URL,也可以在作弊之前简单地捕获并跳过它,因为我的目标是正确处理异常。有人可以建议我如何处理此异常吗?
供参考,这是我的完整循环:
for j in range(0, len(articlelinks)):
try:
req=urllib.request.Request(articlelinks[j], None, {'User-agent' : 'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0'})
response = urllib.request.urlopen(req)
except urllib.request.HTTPError as inst:
brokenlinksflag = 1
brokenlinks = articlelinks[j] + ' ' + format(inst) + ', ' + brokenlinks
continue
except TimeoutError:
brokenlinksflag = 1
brokenlinks = articlelinks[j] + ' Timeout Error, ' + brokenlinks
continue
except urllib.error.URLError as inst:
brokenlinksflag = 1
brokenlinks = articlelinks[j] + ' ' + format(inst) + ', ' + brokenlinks
continue
except SocketError as inst:
brokenlinksflag = 1
brokenlinks = articlelinks[j] + ' ' + sys.exc_info()[0] + ', ' + brokenlinks
continue
except:
print("This article killed everything: " + articlelinks[j])
exit()
Run Code Online (Sandbox Code Playgroud)
解决了!问题是我正在对连接进行故障排除以处理 ConnectionResetError,但是,对完整错误的更仔细检查表明错误是通过尝试处理响应而不是打开 url 引发的:
File "exceptionerror.py", line 97, in <module>
raw_response = response.read().decode('utf8', errors='ignore')
Run Code Online (Sandbox Code Playgroud)
由于连接被重置,而不是完全终止,脚本能够成功打开 URL,并且在尝试解码响应时生成错误,这意味着 try/except 条件在错误的行附近。
以下解决了该问题:
try:
raw_response = response.read().decode('utf8', errors='ignore')
except ConnectionResetError:
brokenlinksflag = 1
brokenlinks = articlelinks[j] + ' ConnectionResetError, ' + brokenlinks
continue
Run Code Online (Sandbox Code Playgroud)