Beautifulsoup Series Selector

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

Next choice

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

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

+16
python beautifulsoup
source share
3 answers

Use css selectors instead:

 soup.select('div.A.B') 
+19
source share

You can use CSS selectors instead, which is probably the best solution here.

 soup.select("div.classname1.classname2") 

You can also use the function .

 def interesting_tags(tag): if tag.name == "div": classes = tag.get("class", []) return "A" in classes and "B" in classes soup.find_all(interesting_tags) 
+6
source share

1 some tags like:

 <span class="ABCD">XXXX</span> 

if you want to use the CSS selector to get the tag, you can write code for the class attribute as follows:

 spans = beautifulsoup.select('span.ABCD') 

2 And if you want to use this for the id attribute, change it like this:

 <span id="A">XXXX</span> 

change the character you use in the select function:

 span = beautifulsoup.select('span#A') 

What we learned is that its grammar is similar to CSS3

+1
source share

All Articles