Beautiful Soup 'ResultSet' object has no attribute 'text'

本秂侑毒 提交于 2020-04-07 03:00:43

问题


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

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


回答1:


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



回答2:


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. 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 = soup.find('div', {"class":"wrapper"})
div.text



回答3:


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.

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



来源:https://stackoverflow.com/questions/36091242/beautiful-soup-resultset-object-has-no-attribute-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!