0

函数看起来像:

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异常?

4

1 回答 1

1

因为它与 urllib2.URLError 无关,与文件“scrape.py”中的异常有关。

您用于 csv.reader 的文件“scrape.py”中的错误情况没有得到很好的处理。

您按照此处的规定使用以下内容:

import socket

try:
    resp = urllib2.urlopen(req, timeout=5)
except urllib2.URLError:
    print "Bad URL or timeout"
except socket.timeout:
    print "socket timeout"
于 2013-04-12T03:31:45.220 回答