我正在 try catch 错误 60 并继续执行我的脚本,这是我目前正在做的事情:
import urllib2
import csv
from bs4 import BeautifulSoup
matcher = csv.reader(open('matcher.csv', "rb" ))
for i in matcher:
url = i[1]
if len(list(url)) > 0:
print url
try:
soup = BeautifulSoup(urllib2.urlopen(url,timeout=10))
except urllib2.URLError, e:
print ("There was an error: %r" % e)
它返回这个:
Traceback (most recent call last): File "debug.py", line 13, in soup = BeautifulSoup(urllib2.urlopen(url,timeout=10)) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 400, in open response = self._open(req, data) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 418, in _open '_open', req) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 378, in _call_chain result = func(*args) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1207, in http_open return self.do_open(httplib.HTTPConnection, req) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1180, in do_open r = h.getresponse(buffering=True) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1030, in getresponse response.begin() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 407, in begin version, status, reason = self._read_status() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 365, in _read_status line = self.fp.readline() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 447, in readline data = self._sock.recv(self._rbufsize) socket.timeout: timed out
我如何捕获此错误并“继续”?
最佳答案
您可以导入异常对象并修改您的 except
block :
import socket
try:
soup = BeautifulSoup(urllib2.urlopen(url,timeout=10))
except urllib2.URLError as e:
print ("There was an error: %r" % e)
except socket.timeout as e: # <-------- this block here
print "We timed out"
更新:嗯,学到了一些新东西 - 刚刚找到对 .reason
属性的引用:
except urllib2.URLError as e:
if isinstance(e.reason, socket.timeout):
pass # ignore this one
else:
# do stuff re other errors if you can...
raise # otherwise propagate the error
关于python - 使用 urllib 2 捕获错误 60(超时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13284553/