这是一段网络挖掘脚本。
def printer(q,missing):
while 1:
tmpurl=q.get()
try:
image=urllib2.urlopen(tmpurl).read()
except httplib.HTTPException:
missing.put(tmpurl)
continue
wf=open(tmpurl[-35:]+".jpg","wb")
wf.write(image)
wf.close()
q
是一个由 Url 组成的队列,missing
是一个空队列,用于收集发生错误的 Url。
它通过 10 个线程并行运行。
每次运行时,我都会得到以下结果。
File "C:\Python27\lib\socket.py", line 351, in read
data = self._sock.recv(rbufsize)
File "C:\Python27\lib\httplib.py", line 541, in read
return self._read_chunked(amt)
File "C:\Python27\lib\httplib.py", line 592, in _read_chunked
value.append(self._safe_read(amt))
File "C:\Python27\lib\httplib.py", line 649, in _safe_read
raise IncompleteRead(''.join(s), amt)
IncompleteRead: IncompleteRead(5274 bytes read, 2918 more expected)
但我确实使用了except
...我尝试了其他一些东西,比如说
httplib.IncompleteRead
urllib2.URLError
甚至,
image=urllib2.urlopen(tmpurl,timeout=999999).read()
但这些都没有起作用...
我该如何捕获IncompleteRead
和URLError
?