gpt4 book ai didi

python - Python3 中的 http.client.IncompleteRead 错误

转载 作者:行者123 更新时间:2023-12-01 09:16:19 47 4
gpt4 key购买 nike

我正在尝试使用 beautifulsoup4 和 python3 来抓取 really long web page。由于网站的大小,当我尝试在网站中搜索某些内容时,http.client 会抛出错误:

File "/anaconda3/lib/python3.6/http/client.py", line 456, in read return self._readall_chunked() File "/anaconda3/lib/python3.6/http/client.py", line 570, in _readall_chunked raise IncompleteRead(b''.join(value)) http.client.IncompleteRead: IncompleteRead(16109 bytes read)

有什么办法可以解决这个错误吗?

最佳答案

作为 http.client 的文档在顶部告诉您,这是一个非常低级的库,主要用于支持 urllib ,并且:

See also The Requests package is recommended for a higher-level HTTP client interface.

如果您可以conda install requestspip install requests,您的问题就变得微不足道:

import requests
req = requests.get('https://www.worldcubeassociation.org/results/events.php?eventId=222&regionId=&years=&show=All%2BPersons&average=Average')
soup = BeautifulSoup(req.text, 'lxml')

如果您无法安装第三方库,解决此问题是可能的,但实际上不受支持,而且并不容易。 http.client 中的 block 处理代码都不是公开的或记录的,但文档确实将您链接到 the source ,您可以在其中看到私有(private)方法。特别要注意的是,read 调用了一个名为 _readall_chunked 的方法,该方法循环调用 _get_chunk_left 上的 _safe_read 方法。该 _safe_read 方法是您需要替换的代码(例如,通过子类化 HTTPResponse 或对其进行猴子修补)来解决此问题。这可能不会像使用更高级别的库那么简单或有趣。

关于python - Python3 中的 http.client.IncompleteRead 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51226635/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com