Beautifulsoup multiple class selector

Botond picture Botond · Oct 28, 2016 · Viewed 20.8k times · Source

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.

Answer

lucasnadalutti picture lucasnadalutti · Oct 28, 2016

Use css selectors instead:

soup.select('div.A.B')