首页 > 解决方案 > “utf-8”编解码器无法解码字节 0xd7:无效的继续字节

问题描述

注意:此测试作为目标站点错误赏金计划的一部分运行。

我正在尝试使用requestsin解析从 Google 返回的结果Python3

我遇到的问题是我收到以下错误:

'utf-8' codec can't decode byte 0xd7 in position 5698: invalid continuation byte

这是相关代码:

def __init__(self):
    self.soup = BeautifulSoup(features="html.parser")
    self.query_addr = "http://www.google.com/search?"

def parse_search_results(self, res):
    links = []
    soup = self.soup(res) # This call triggers the error
    no_results = soup.find("did not match any documents.")

截屏:

我将res(这是一个字节对象)的结果保存在一个 html 文件中并在浏览器中查看它。您可以看到呈现的违规字节。

在此处输入图像描述

这是res没有封闭的价值b'',我已经把它放在了Github上。

我也没有decode在我的代码中的任何地方显式调用或指定utf-8. 我的理解是它BeautifulSoup自己处理。

我需要做什么才能BeautifulSoup正确解析这些数据?

标签: python-3.xbeautifulsouppython-requests

解决方案


推荐阅读