作者:懷捻過呿_649 | 来源:互联网 | 2023-09-24 09:31
我正在制作一个在网页上搜索单词的Python程序.虽然,当我尝试websiteurllib.request.urlopen(url)contentwebsite.read()web
我正在制作一个在网页上搜索单词的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>", "")
TypeError: a bytes-like object is required, not 'str'
我是新来的Python,所以我不知道如何处理此错误.
Python 3.5,Mac.
解决方法:
使用Charset头部(reference)内发送的字符集解码()内容:
resource = urllib.request.urlopen(url)
cOntent= resource.read()
charset = resource.headers.get_content_charset()
cOntent= content.decode(charset)
适合我(Python 3.5,Mac OS).