How to soup particular div class when there is another with similar one?

Viewed 51
from bs4 import BeautifulSoup as bs
x = ' <div class="data dturd"><h3>Seaspiracy</h3></div>  <div class="data"><h3>SeaspiracyX</h3></div>'
soup = bs(x,"lxml")
print(soup.find('div',class_='data'))

I am trying to soup the second div with class 'data' in it. But the above code is always finding the div with class 'data dturd'.

How can I solve it ?

4 Answers

You could use a CSS selector with .select_one and the :nth-child(2) pseudoselector:

>>> soup.select_one(".data:nth-child(2)")
<div class="data"><h3>SeaspiracyX</h3></div>

You can use :not to filter out for the other class present in the first of the children

from bs4 import BeautifulSoup as bs
x = ' <div class="data dturd"><h3>Seaspiracy</h3></div>  <div class="data"><h3>SeaspiracyX</h3></div>'
soup = bs(x,"lxml")
print(soup.select_one('.data:not(.dturd)').text)

I definetly agree with @ggorlen's approch. Here is a more clumpsy approach, that somewhat works.

from bs4 import BeautifulSoup as bs
x = ' <div class="data dturd"><h3>Seaspiracy</h3></div>  <div class="data"><h3>SeaspiracyX</h3></div>'
soup = bs(x,"lxml")
print(soup.find_all('div',class_='data')[1])

And another solution:

soup.find(lambda tag: tag.name == "div" and tag['class'] == ["data"])
Related