处理 IncompleteRead,URLError

Handling IncompleteRead,URLError

提问人:from __future__ 提问时间:8/13/2012 最后编辑:shx2from __future__ 更新时间:6/1/2022 访问量:7415

问:

这是一个网络挖掘脚本。

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 组成的,用于收集 error-raising-urls 的空队列Queue()

它由 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()

但这些都不起作用。

我怎样才能抓住 和 ?IncompleteReadURLError

python 错误处理 urllib2 httplib

评论

0赞 Vincent Ketelaars 2/13/2014
有点晚了,但首先在谷歌上点击。所以,stackoverflow.com/a/14206036/1444854 应该解决你的问题。顺便说一句,通常如果你想捕获多个异常,请将它们放在一个元组中:except (httplib.IncompleteRead,urllib2。URLError)

答:

0赞 Michael Leonard 10/22/2015 #1

我认为这个问题的正确答案取决于您认为的“引起错误的 URL”。

捕获多个异常的方法

如果您认为应该将引发异常的任何 URL 添加到队列中,那么您可以执行以下操作:missing

try:
    image=urllib2.urlopen(tmpurl).read()
except (httplib.HTTPException, httplib.IncompleteRead, urllib2.URLError):
    missing.put(tmpurl)
    continue

这将捕获这三个异常中的任何一个,并将该 url 添加到队列中。更简单地说,您可以做到:missing

try:
    image=urllib2.urlopen(tmpurl).read()
except:
    missing.put(tmpurl)
    continue

捕获任何异常,但这不被视为 Pythonic,并且可能会隐藏代码中其他可能的错误。

如果“引发错误的 URL”是指任何引发错误的 URL,但在收到其他错误时您仍希望继续处理,那么您可以执行以下操作:httplib.HTTPException

try:
    image=urllib2.urlopen(tmpurl).read()
except httplib.HTTPException:
    missing.put(tmpurl)
    continue
except (httplib.IncompleteRead, urllib2.URLError):
    continue

这只会在 URL 引发 但会捕获并防止脚本崩溃时将 URL 添加到队列中。missinghttplib.HTTPExceptionhttplib.IncompleteReadurllib.URLError

遍历队列

顺便说一句,循环对我来说总是有点令人担忧。您应该能够使用以下模式循环浏览队列内容,但您可以自由地继续按照自己的方式执行此操作:while 1

for tmpurl in iter(q, "STOP"):
    # rest of your code goes here
    pass

安全地处理文件

顺便说一句,除非绝对有必要这样做,否则您应该使用上下文管理器来打开和修改文件。因此,您的三个文件操作行将变为:

with open(tmpurl[-35:]+".jpg","wb") as wf:
    wf.write()

上下文管理器负责关闭文件,即使在写入文件时发生异常时也会这样做。