Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beautifulsoup multiple class selector

I want to select all the divs which have BOTH A and B as class attributes.

The following selection

soup.findAll('div', class_=['A', 'B']) 

however selects all the divs which have EITHER A or B in their class attributes. Classes may have many other attributes (C, D, etc) in any order, but I want to select only those ones that have both A and B.

like image 886
Botond Avatar asked Oct 28 '16 12:10

Botond


People also ask

How do I search for multiple classes in BeautifulSoup?

get(link) soup = BeautifulSoup(r. text) items = soup. findAll(True, {"class":["equal achievements number", "up achievements number"]}) This one is applying Roman Pekar's solution, but it returns an empty list. The same thing happens using the regular expression solution.


1 Answers

Use css selectors instead:

soup.select('div.A.B') 
like image 107
lucasnadalutti Avatar answered Oct 11 '22 10:10

lucasnadalutti