Skip to content Skip to sidebar Skip to footer

Is There Any Strict FindAll Function In BeautifulSoup?

I am using Python- 2.7 and BeautifulSoup Apologies if I am unable to explain what exactly I want There is this html page in which data is embedded in specific structure I want to

Solution 1:

Or, you may make a CSS selector to match the class exactly to listing-row:

soup.select("div[class=listing-row]")

Demo:

>>> from bs4 import BeautifulSoup
>>> 
>>> data = """
... <div>
...     <div class="listing-row">result1</div>
...     <div class="listing-row wide-featured-listing">result2</div>
...     <div class="listing-row">result3</div>
... </div>
... """
>>> 
>>> soup = BeautifulSoup(data, "html.parser")
>>> print [row.text for row in soup.select("div[class=listing-row]")]
[u'result1', u'result3']

Solution 2:

You could just filter out that element:

self.tab = [el for el in soup.find_all('div', class_='listing-row')
            if 'wide-featured-listing' not in el.attr['class']]

You could use a custom function:

self.tab = soup.find_all(lambda e: e.name == 'div' and
                                   'listing-row' in e['class'] and
                                   'wide-featured-listing' not in el.attr['class'])

Post a Comment for "Is There Any Strict FindAll Function In BeautifulSoup?"