Python http.client.Incomplete Read(0 bytes read) 错误

Cha*_*yer 9 urllib beautifulsoup web-scraping python-3.x

我在论坛上看到了这个错误并阅读了回复,但我仍然不明白它是什么或如何解决它。我正在从互联网上的 16k 个链接中抓取数据,我的脚本从每个链接中抓取类似的信息并将其写入 .csv 中,其中一些日期是在此错误之前写入的。

Traceback (most recent call last):
 File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 541, in _get_chunk_left
   chunk_left = self._read_next_chunk_size()
 File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 508, in _read_next_chunk_size
   return int(line, 16)
ValueError: invalid literal for int() with base 16: b''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
 File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 558, in _readall_chunked
   chunk_left = self._get_chunk_left()
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 543, in _get_chunk_left
   raise IncompleteRead(b'')
http.client.IncompleteRead: IncompleteRead(0 bytes read)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "MoviesToDb.py", line 91, in <module>
html = r.read()
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 455, in read
   return self._readall_chunked()
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 565, in _readall_chunked
   raise IncompleteRead(b''.join(value))
http.client.IncompleteRead: IncompleteRead(17891 bytes read)
Run Code Online (Sandbox Code Playgroud)

我想知道:
1)这个错误是什么意思?
2)我该如何预防?

小智 7

尝试导入:

from http.client import IncompleteRead
Run Code Online (Sandbox Code Playgroud)

并将其添加到您的脚本中:

except IncompleteRead:
    # Oh well, reconnect and keep trucking
        continue
Run Code Online (Sandbox Code Playgroud)