Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check if element exists or not

I have a code that grabs a price tag from this HTML section

<div class="main">
    <div class="cost-box">
        <ins><span>$</span><price>10.00</price></ins>
    </div>
</div>

Here’s the code I use to get the 10.00 price:

import requests
from bs4 import BeautifulSoup as bs

url = "https://www.sample.com/sample/123abcd"

response = requests.get(url).text
soup = bs(response, "html.parser")

container = soup.find("div", class_="cost-box")
price = container.price    # <-- get <price> tag from container
print(price.text)

The only problem though is that some pages doesn’t have prices on them and would only have something like this in their HTML:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<div class="cost-box">
   
</div>

And my code would now give an error saying

price = container.price.text
AttributeError: 'NoneType' object has no attribute 'text'

Is there a way to add some sort of a checkpoint on whether the price variable exists? Instead of having the error and the whole program coming to a stop, I just want the price to say invalid and the code would still continue (I placed it on a for loop).

>Solution :

You can use if-else:

import requests
from bs4 import BeautifulSoup as bs

# price missing:
html_text = """
<div class="main">
    <div class="cost-box">

    </div>
</div>"""

soup = bs(html_text, "html.parser")

container = soup.find("div", class_="cost-box")
price = container.price.text if container.price else "Invalid"
print(price)

Prints:

Invalid
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading