python - Beautiful Soup 'ResultSet' 对象没有属性 'text'

标签 python beautifulsoup

from bs4 import BeautifulSoup
import urllib.request
import win_unicode_console
win_unicode_console.enable()


link = ('https://pietroalbini.io/')  
req = urllib.request.Request(link, headers={'User-Agent': 'Mozilla/5.0'})
url = urllib.request.urlopen(req).read()

soup =  BeautifulSoup(url, "html.parser")
body = soup.find_all('div', {"class":"wrapper"})

print(body.text)

你好,我有 Beautiful Soup 的问题,如果我在末尾没有“.text”的情况下运行这段代码,它会显示一个 div 列表,但如果我在末尾添加“.text”,则会出现错误

Traceback (most recent call last): File "script.py", line 15, in print(body.text) AttributeError: 'ResultSet' object has no attribute 'text'

最佳答案

find_all 返回一个 ResultSet 对象,您可以使用 for 循环对其进行迭代。你可以做的是:

for wrapper in body.find_all('div', {"class":"wrapper"}):
   print wrapper.text

关于python - Beautiful Soup 'ResultSet' 对象没有属性 'text',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36091242/

相关文章:

python - 重定义对象的方法

python - 使用 unicode 时颜色条标签截止

python - 如何使用正则表达式检索非标准关键字属性的值,以将属性的值与 beautifulsoup 进行匹配?

python - BS4,在未闭合的 <br> 之间进行精确匹配

python - 获取网页内容(不是来自源代码)

python - numpy 中不带循环的类标签求和

python - 在 Pandas 中将字典转换为对称/距离矩阵的最有效方法

python - Hive 转换使用 Python : Unable to initialize custom script

python - 在 Python 中使用 BeautifulSoup 从 HTML 中删除回车符

python - 正则表达式或其他返回这些值的方法(来自 BeautifulSoup)