功能如下:
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
异常?
答案 0 :(得分:1)
因为它与urllib2.URLError无关,与文件“scrape.py”中的异常有关。
您用于csv.reader的文件“scrape.py”中的错误情况处理不当。
您按照规定here使用以下内容:
import socket
try:
resp = urllib2.urlopen(req, timeout=5)
except urllib2.URLError:
print "Bad URL or timeout"
except socket.timeout:
print "socket timeout"