简体   繁体   中英

prettify works on Soup but not on Soup1=Soup.find_all

MyAlphaSoup = BeautifulSoup(website_to_crawl.text, 'html.parser')

MyAlphaSoup.prettify()

MyAlphaAlphaSoup = MyAlphaSoup.find_all("div", {"class": "lister-item featurefilm"})

MyAlphaAlphaSoup.prettify()

I am getting an error at the 4th line

raise AttributeError( AttributeError: ResultSet object has no attribute 'prettify'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

Isn't soup.find_all merely reducing the soup to a smaller subset? so the type isn't changing?

Error

Code Pic

That's because the function find_all returns a list. So, you should be able to do that if you specify the element of the list or loop through them:

my_alpha_soup = BeautifulSoup(website_to_crawl.text, 'html.parser')

my_alpha_soup.prettify()

list_of_elements = my_alpha_soup.find_all("div", {"class": "lister-item featurefilm"})

for element in list_of_elements:
    print(element.prettify())

Also, is a convention to only use initial capital letters for classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM