gpt4 book ai didi

python - 不能对 html2text 使用 read() 吗?

转载 作者:太空宇宙 更新时间:2023-11-03 11:25:40 25 4
gpt4 key购买 nike

我正在制作一个 Python 程序,用于在网页中搜索单词。虽然,当我尝试

website = urllib.request.urlopen(url)
content = website.read()
website.close()
test = html2text.html2text(content)
print(test)

我收到这个错误:

test = html2text.html2text(content)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site- packages/html2text/__init__.py", line 840, in html2text
return h.handle(html)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site- packages/html2text/__init__.py", line 129, in handle
self.feed(data)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/html2text/__init__.py", line 125, in feed
data = data.replace("</' + 'script>", "</ignore>")
TypeError: a bytes-like object is required, not 'str'

我是 Python 的新手,所以我不确定如何处理这个错误。
Python 3.5、Mac。

最佳答案

decode() Charset header ( reference ) 中发送的带有字符集的内容:

resource = urllib.request.urlopen(url)
content = resource.read()
charset = resource.headers.get_content_charset()
content = content.decode(charset)

适用于我(Python 3.5、Mac OS)。

关于python - 不能对 html2text 使用 read() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34476403/

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