【问题标题】:Python http.client.Incomplete Read(0 bytes read) errorPython http.client.Incomplete Read(0 bytes read) 错误
【发布时间】:2017-01-08 03:00:45
【问题描述】:

我在论坛上看到了这个错误并阅读了回复,但我仍然不明白它是什么或如何解决它。我正在从互联网上从 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)

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

【问题讨论】:

    标签: python-3.x web-scraping beautifulsoup urllib


    【解决方案1】:

    尝试导入:

    from http.client import IncompleteRead
    

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

    except IncompleteRead:
        # Oh well, reconnect and keep trucking
            continue
    

    【讨论】:

    • 如何在气流中将其与 docker 合并?现在我在下载 docker 层时遇到了这个问题。
    【解决方案2】:

    requests.exceptions.ChunkedEncodingError: (‘Connection broken: IncompleteRead(0 bytes read)’, IncompleteRead(0 bytes read)).
    

    因为http协议的服务器是1.0版本,而python使用的是1.1版本。解决方案是分配客户端的协议版本,像这样

    Python3版本请补充:

    > import http.client
    > http.client.HTTPConnection._http_vsn = 10
    > http.client.HTTPConnection._http_vsn_str = 'HTTP/1.0'
    

    Python2版本请补充:

    > import http.client
    > http.client.HTTPConnection._http_vsn = 10
    > http.client.HTTPConnection._http_vsn_str = 'HTTP/1.0'
    

    见参考How to deal with "http.client.IncompleteRead: IncompleteRead(0 bytes read)" problem

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-06
      • 2012-11-20
      • 2018-08-10
      • 1970-01-01
      • 2020-02-07
      • 2023-01-20
      • 1970-01-01
      • 2021-02-18
      相关资源
      最近更新 更多