函数看起来像:
def fetchurl(url):
timeout = 10
try:
res = urllib2.urlopen(url, timeout=timeout)
reader = csv.reader(res)
reader.next() # Trim the CSV header
return reader
except urllib2.URLError, e:
print 'bailing on %s (timeout of %s exceeded)' % (url, timeout)
return None
异常看起来像:
File "scrape.py", line 35, in fetchurl
reader.next() # Trim the CSV header
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 530, in next
line = self.readline()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 447, in readline
data = self._sock.recv(self._rbufsize)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 541, in read
return self._read_chunked(amt)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 601, in _read_chunked
value.append(self._safe_read(chunk_left))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 647, in _safe_read
chunk = self.fp.read(min(amt, MAXAMOUNT))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 380, in read
data = self._sock.recv(left)
socket.timeout: timed out
为什么 try/except 块没有捕获socket.timeout
异常?