这是一段网络挖掘脚本。
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
是一个Queue()
由 Urls 和 `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
and URLError
?