How to Find a tag without specific attribute using beautifulsoup?

Viewed 4330

I'm trying to get the content of the 'p' tags that didn't have the specific attribute.

I have some tags with 'class'='cost', and some tags with 'class'='cost' and 'itemprop'='price'

all_cars = soup.find_all('div', attrs={'class': 'listdata'})
...
...
tatal_cost= car.findChildren('p', attrs={'class': 'cost'})
cost= car.findChildren('p', attrs={'class': 'cost', 'itemprop':'price'})

I am trying to find 'p' tags without 'itemprop' attribute, but i cant find any solution.

2 Answers

BeautifulSoup's built-in attribute filters are enough for this. You can give True as value to simple check if the attribute is present. None can be used to specify that the attribute should not be present. Likewise the value can be any attribute value (eg 'cost').

from bs4 import BeautifulSoup
html="""
<p class="cost">paragraph 1</p>
<p class="cost">paragraph 2</p>
<p class="cost">paragraph 3</p>
<p class="cost" itemprop="1">paragraph 4</p>
<p class="somethingelse">paragraph 5</p>
"""
soup=BeautifulSoup(html,'html.parser')
print("---without 'itemprop' attribute")
print(soup.find_all('p',itemprop=None))
print("---with class = 'cost' and without 'itemprop' attribute----")
print(soup.find_all('p',attrs={'itemprop':None,"class":'cost'}))
#below is an alternative way to specify this
#print(soup.find_all('p',itemprop=None,class_='cost'))

Output

---without 'itemprop' attribute
[<p class="cost">paragraph 1</p>, <p class="cost">paragraph 2</p>, <p class="cost">paragraph 3</p>, <p class="somethingelse">paragraph 5</p>]
---with class = 'cost' and without 'itemprop' attribute----
[<p class="cost">paragraph 1</p>, <p class="cost">paragraph 2</p>, <p class="cost">paragraph 3</p>]

BeautifulSoup lets you define a function and pass it into its find_all() method:

def has_class_but_not_itemprop(tag):
    return tag.has_attr('class') and not tag.has_attr('itemprop')

# Pass this function into find_all() and you’ll pick up all the <p> 
# tags you're after:

soup.find_all(has_class_but_not_itemprop)
# [<p class="cost">...</p>,
#  <p class="cost">...</p>,
#  <p class="cost">...</p>]

For more information, see the BeautifulSoup documentation.

Related