“解码错误输出不是UTF8”来自HTML

2024-06-28 19:56:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python中运行BS4,代码如下:

import urllib
import requests
from bs4 import BeautifulSoup
from bs4 import UnicodeDammit

with requests.Session() as s:
     page = s.get(url)  

doc = page.text

soup = BeautifulSoup(doc, "html.parser")
print(soup)

这将生成错误:[解码错误-输出不是utf-8]

使用以下方法会产生相同的错误:

^{pr2}$

使用忽略:

soup = BeautifulSoup(page.read().decode('utf-8', 'ignore'))

给我不同的错误:

AttributeError: 'Response' object has no attribute 'read'


Tags: 代码fromimportreaddoc错误pageurllib