Beautiful Soup 'ResultSet' 对象没有属性 'text' [英] Beautiful Soup 'ResultSet' object has no attribute 'text'

查看:32
本文介绍了Beautiful Soup 'ResultSet' 对象没有属性 'text'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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"就会出现错误

Hi, I have a problem with Beautiful Soup, if I run this code without ".text" at the end it show me a list of div but if I add ".text" at the end come the error

回溯(最近一次调用最后一次):文件script.py",第 15 行,在打印(正文.文本)AttributeError: 'ResultSet' 对象没有属性 '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 循环对其进行迭代.你可以做的是:

find_all returns a ResultSet object which you can iterate over using a for loop. What you can do is:

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

这篇关于Beautiful Soup 'ResultSet' 对象没有属性 'text'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆