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

查看:212
本文介绍了美丽的汤'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

回溯(最近通话最近): 在第15行的文件"script.py"中 打印(body.text) 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

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

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