警告:root:某些字符无法解码,并被替换为 REPLACEMENT CHARACTER。随着请求和Beastuifulsoup

Nic*_*k M 11 python encoding beautifulsoup request

几分钟前我有这个网络抓取代码工作,但现在我收到了这个警告和编码。由于此请求不返回 html,因此当我搜索标签内容时,Beautifulsoup 将返回 None 类型。这里出了什么问题?我试图用谷歌搜索这个编码问题,但找不到明确的答案。

import requests
from bs4 import BeautifulSoup


url = 'http://finance.yahoo.com/q?s=aapl&fr=uh3_finance_web&uhb=uhb2'

data = requests.get(url)
soup = BeautifulSoup(data.content).text
print(data)
Run Code Online (Sandbox Code Playgroud)

结果如下:

0.0 seconds
WARNING:root:Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.
<Response [200]>
WARNING:root:Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.
<Response [200]>
WARNING:root:Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.
<Response [200]>
WARNING:root:Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.
<Response [200]>
WARNING:root:Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.
<Response [200]>
WARNING:root:Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.
<Response [200]>
WARNING:root:Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.
<Response [200]>
WARNING:root:Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.
<Response [200]>
WARNING:root:Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.
<Response [200]>
WARNING:root:Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.
<Response [200]> 
{}

Process finished with exit code 0
Run Code Online (Sandbox Code Playgroud)

tol*_*maz 17

下面的 Beautifulsoup 的构造函数对我有用:

soup = BeautifulSoup(open(html_path, 'r'),"html.parser",from_encoding="iso-8859-1")
Run Code Online (Sandbox Code Playgroud)