Check If a Specific Class and Value Exist in HTML Using Beautifulsoup Python

Check if a specific class present in HTML using beautifulsoup Python

has_attr is a method that checks if an element has the attribute that you want. class is an attribute, i-stars i-stars--large-3 rating-very-large is its value.

find expects CSS selectors, not class values. So you should instead use html.find('div.i-stars.i-stars--large-3.rating-very-large'). This is because you are looking for a div with all of these classes.

Beautifulsoup if class exists

You're very close... soup.findall will return an empty list if it doesn't find any matches. Your control statement is checking its return for a literal bool value. Instead you need to check its truthiness by omitting the ==True

if soup.find_all("div", {"class": "info"}):
print("Tag Found")

Python - bs4, check if class has a value

To check if an element has disabled and RevealButton classes, you could use the dictionary-like interface of BeautifulSoup elements (Tag instances):

"disabled" in element["class"] and "RevealButton" in element["class"]

Note: you need to apply this on the option element.

Note that class is a special multi-valued attribute and its value is a list.


Another option (no pun intended) would be to look for option element with both classes:

for select_tag in select_tags:
if select_tag.select("option.disabled.RevealButton"):
continue

print(select_tag)

Here, option.disabled.RevealButton is a CSS selector that would match option elements having both disabled and RevealButton classes.

How to use BeautifulSoup to find specific class elements on a web page

I'm not sure why this affects it but it actually has to do with how you're encoding your html, or rather the end format of your html that you're using to run the search.

Add '&qs=n&form=QBRE&=%25eManage%20Your%20Search%20History%25E&sp=-1&p' to the end of your url variable, and I bet your code will find those class items now.

BeautifulSoup - check if elements have specific class

You were almost there.

for node in soup.find_all(attrs={"class": re.compile(r'my_class(1|2)')}):
print(node)

Checking for attributes in BeautifulSoup?

Besides using get() method

n.get("class")

Another option is to use has_attr() (use has_key() pre BeautifulSoup 4):

n.has_attr("class")

Test if children tag exists in beautifulsoup

The simplest way to find if a child tag exists is simply

childTag = xml.find('childTag')
if childTag:
# do stuff

More specifically to OP's question:

If you don't know the structure of the XML doc, you can use the .find() method of the soup. Something like this:

with open("file1.xml",'r') as data, open("file2.xml",'r') as data2:
xml = BeautifulSoup(data.read())
xml2 = BeautifulSoup(data2.read())

hasAttrBs = xml.find("myId")
hasAttrBs2 = xml2.find("myId")

If you do know the structure, you can get the desired element by accessing the tag name as an attribute like this xml.document.subdoc.myid. So the whole thing would go something like this:

with open("file1.xml",'r') as data, open("file2.xml",'r') as data2:
xml = BeautifulSoup(data.read())
xml2 = BeautifulSoup(data2.read())

hasAttrBs = xml.document.subdoc.myid
hasAttrBs2 = xml2.document.subdoc.myid
print hasAttrBs
print hasAttrBs2

Prints

<myid>1</myid>
None


Related Topics



Leave a reply



Submit