gpt4 book ai didi

python - 无法使用 requests.get(url) 运行 BeautifulSoup

转载 作者:太空宇宙 更新时间:2023-11-04 08:17:07 26 4
gpt4 key购买 nike

start_url=requests.get('http://www.delicious.com/golisoda')
soup=BeautifulSoup(start_url)

此代码显示以下错误:

Traceback (most recent call last):
File "test2_requests.py", line 10, in <module>
soup=BeautifulSoup(start_url)
File "/usr/local/lib/python2.7/dist-packages/bs4/__init__.py", line 169, in __init__
self.builder.prepare_markup(markup, from_encoding))
File "/usr/local/lib/python2.7/dist-packages/bs4/builder/_lxml.py", line 68, in prepare_markup
dammit = UnicodeDammit(markup, try_encodings, is_html=True)
File "/usr/local/lib/python2.7/dist-packages/bs4/dammit.py", line 203, in __init__
self._detectEncoding(markup, is_html)
File "/usr/local/lib/python2.7/dist-packages/bs4/dammit.py", line 373, in _detectEncoding
xml_encoding_match = xml_encoding_re.match(xml_data)
TypeError: expected string or buffer

最佳答案

使用响应的.content:

start_url = requests.get('http://www.delicious.com/golisoda')
soup = BeautifulSoup(start_url.content)

或者,您可以使用解码后的 unicode 文本:

start_url = requests.get('http://www.delicious.com/golisoda')
soup = BeautifulSoup(start_url.text)

参见 Response content section的文档。

关于python - 无法使用 requests.get(url) 运行 BeautifulSoup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12478965/

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