Skip to content Skip to sidebar Skip to footer

Beautifulsoup4: Select Elements Where Attributes Are Not Equal To X

I'd like to do something like this: soup.find_all('td', attrs!={'class':'foo'}) I want to find all td that do not have the class of foo. Obviously the above doesn't work, what doe

Solution 1:

BeautifulSoup really makes the "soup" beautiful and easy to work with.

You can pass a function in the attribute value:

soup.find_all('td', class_=lambda x: x != 'foo')

Demo:

>>>from bs4 import BeautifulSoup>>>data = """...<tr>...    <td>1</td>...    <td class="foo">2</td>...    <td class="bar">3</td>...</tr>...""">>>soup = BeautifulSoup(data)>>>for element in soup.find_all('td', class_=lambda x: x != 'foo'):...print element.text... 
1
3

Solution 2:

There is a method .select() which allows you to pass CSS selectors as a string:

soup.select('td:not(.foo)')

The above code will return all <td> tags which are not of the class foo.

Post a Comment for "Beautifulsoup4: Select Elements Where Attributes Are Not Equal To X"