有没有办法验证urllib2响应是否完整?我偶尔会收到Httplib.IncompleteRead错误。

时间:2013-05-11 20:59:02

标签: python urllib2 httplib

我有一个函数可以通过urllib2打开一个页面来提取一些数据。它在80%的时间内都运行正常,但是有20%的时间,我会收到IncompleteRead例外。

回溯

Traceback (most recent call last):
  File "test.py", line 380, in <module>
    main()
  File "test.py", line 109, in main
    soups.append(BeautifulSoup(out_queue.get().read()))
  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 601, in _read_chunked
    value.append(self._safe_read(chunk_left))
  File "c:\python27\lib\httplib.py", line 649, in _safe_read
    raise IncompleteRead(''.join(s), amt)
httplib.IncompleteRead: IncompleteRead(958 bytes read, 678 more expected)

我正在通过基本的

打开页面
response = urllib2.urlopen('the_url') 

然后在程序中将其转换为BeautifulSoup对象。

使用urllib2进行初始请求时,是否有办法解决问题?在之前验证数据是否为“整个”的一些方法我尝试用它做一些事情?

1 个答案:

答案 0 :(得分:0)

你可以一次阅读:

try:
    response_data = urllib2.urlopen('the_url').read()
except httplib.IncompleteRead:
    pass # handle the error