简体   繁体   English

美丽的汤'ResultSet'对象没有属性'text'

[英]Beautiful Soup 'ResultSet' object has no attribute '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)

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 嗨,我有一个美丽的汤的问题,如果我运行此代码没有“.text”结束它显示我的div列表,但如果我在结尾添加“.text”来错误

Traceback (most recent call last): File "script.py", line 15, in print(body.text) AttributeError: 'ResultSet' object has no attribute 'text' 回溯(最近一次调用最后一次):文件“script.py”,第15行,打印(body.text)AttributeError:'ResultSet'对象没有属性'text'

find_all returns a ResultSet object which you can iterate over using a for loop. find_all返回一个ResultSet对象,您可以使用for循环迭代它。 What you can do is: 你能做的是:

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

If you'll type: 如果你输入:

print(type(body))

you'll see body is <class 'bs4.element.ResultSet'> It means all the elements that match the class. 你会看到body<class 'bs4.element.ResultSet'>它表示所有与该类匹配的元素。 You can either iterate over them: 你可以迭代它们:

for div in body:
    print(div.text)

Or if you know you only have div, you can use find instead: 或者如果你知道你只有div,你可以使用find代替:

div = soup.find('div', {"class":"wrapper"})
div.text

Probably should have posted as answer.. so as stated in the comments almost verbatim 可能应该发布答案..所以几乎逐字逐句地在评论中说明

Your code should be the following: 您的代码应如下所示:

for div in body: 
    print div.text
    #python3
    #print(div.text)

Or some naming schema to your preference thereof. 或者根据您的偏好设置一些命名方案。

The find_all method returns a generated list ( loosely using the term list here ) of items that beautifulsoup has found matching your criteria after parsing the source webpages html either recursively or non-recursively depending upon how you search. find_all方法返回一个生成的列表(松散地使用这里的术语列表),这些项目是beautifulsoup找到的符合条件的项目,在根据您搜索的方式递归或非递归地解析源网页html之后。

As the error says the resulting set of objects has no attribute text, since it isn't an element but rather a collection of them. 正如错误所说,生成的对象集没有属性文本,因为它不是元素,而是它们的集合。 However, the items inside the resulting set ( should any be found ) do. 但是,结果集内的项目(应该找到)都可以。

You can view the documentation here 您可以在此处查看文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 漂亮的汤-&#39;ResultSet&#39;对象没有属性&#39;text&#39; - Beautiful soup - 'ResultSet' object has no attribute 'text' 美丽的汤:“ ResultSet”对象没有属性“ find_all”吗? - Beautiful Soup: 'ResultSet' object has no attribute 'find_all'? Beautiful Soup WebScraping错误-ResultSet对象没有属性&#39;%s&#39; - Beautiful Soup WebScraping Error - ResultSet object has no attribute '%s' Beautiful Soup 错误 AttributeError: ResultSet object has no attribute 'get - Beautiful Soup Error AttributeError: ResultSet object has no attribute 'get Beautiful Soup 中 span 标签上的 find_all 产生 AttributeError:ResultSet 对象没有属性“get_text” - find_all on span tag in Beautiful Soup yields AttributeError: ResultSet object has no attribute 'get_text' 用 Beautiful Soup 解析跨度:'NoneType' object 没有属性 'text' - Parse span with Beautiful Soup : 'NoneType' object has no attribute 'text' 美丽的汤有错误:&#39;NoneType&#39; 对象没有属性 &#39;text&#39; - beautiful soup with error : 'NoneType' object has no attribute 'text' 美丽的汤:AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;text&#39; - Beautiful soup: AttributeError: 'NoneType' object has no attribute 'text' Python Beautiful Soup“ NavigableString”对象没有属性“ get_text” - Python Beautiful Soup 'NavigableString' object has no attribute 'get_text' 美丽的Soup4&#39;NoneType&#39;对象没有属性&#39;text&#39;错误 - Beautiful Soup4 'NoneType' object has no attribute 'text' Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM