我收到以下错误。我在谷歌上搜索得够多了。但是没有什么能解决我的问题。我的问题似乎与其他问题不同。我正在使用 BeautifulSoup。
我认为下面一行是造成问题的原因。
soup = BeautifulSoup(req.content, 'html.parser').encode("utf-8")
当我试图找到所有具有 holder 类的 div 时:
data = soup.find_all("div", {"class":"holder"})
如果显示如下错误:
Traceback (most recent call last): File "web_crawler.py", line 32, in data = soup.find_all("div", {"class":"holder"}) AttributeError: 'bytes' object has no attribute 'find_all'
编码是否造成了问题?
请您参考如下方法:
将 req.content 值转换为 utf-8 而不是 BS 对象的值。
soup = BeautifulSoup(req.content.encode("utf-8"), 'html.parser')
or
我假设您正在使用 requests 模块来获取 http 响应。
req.encoding = 'utf-8'
soup = BeautifulSoup(req.content, 'html.parser')
