AttributeError:“ bytes”对象在Python中编码后没有属性“ find_all”

Md *_*man 1 python django beautifulsoup

我收到以下错误。我已经在Google上进行了足够的搜索。但是没有什么可以解决我的问题。我的问题似乎与其他人不同。我正在使用BeautifulSoup

我认为以下几行引起了问题。

soup = BeautifulSoup(req.content, 'html.parser').encode("utf-8")
Run Code Online (Sandbox Code Playgroud)

当我试图找到所有div有一个holder课时:

data = soup.find_all("div", {"class":"holder"})
Run Code Online (Sandbox Code Playgroud)

如果显示以下错误:

追溯(最近一次通话最近):文件“ web_crawler.py”,第32行,数据= soup.find_all(“ div”,{“ class”:“ holder”}))AttributeError:'bytes'对象没有属性'find_all '

是在encoding制造问题吗?

Avi*_*Raj 5

req.content值转换utf-8为非BS对象的值。

soup = BeautifulSoup(req.content.encode("utf-8"), 'html.parser')

or
Run Code Online (Sandbox Code Playgroud)

我假设您正在使用requests模块来获取http响应。

req.encoding = 'utf-8'
soup = BeautifulSoup(req.content, 'html.parser')
Run Code Online (Sandbox Code Playgroud)