anonymous
anonymous

Reputation: 1330

Multiple selection Beautiful Soup

I have a class named summary and it contains a lot of differents tags like 'p', 'h2', 'img', 'li' etc...

What I want to do is a search using that criteria. I've tried using select and find_all with no luck.

Select: data = soup.select('summary p') but I cant include more than one tag at once eg: h2

Find_all: data = soup.find_all(['p', 'h2']) here I can pass a list and it finds all tags, but I don't know how to narrow the search to the summary class

How can I do it?

Thank you in advance!

Upvotes: 2

Views: 2846

Answers (1)

shaktimaan
shaktimaan

Reputation: 12092

Your approach with find_all() is correct. You just have to pass the class as an attribute to the find_all(). Like this:

data = soup.find_all(['p', 'h2'], attrs={'class':'summary'})

It is documented here

Upvotes: 2

Related Questions