Python BeautifulSoup check if find returns Null object

+1 vote

Hi. I am new to python web scraping. I am trying to scrape a website and store data. The data I am trying to extract is under the item-name class and between the anchor tags. I did a little research and found a code that I could use.

name=page.find('a',{'class':'item-name'}).getText()

The problem is that in some case, there is no Object for this class. So it the script is showing an error telling the object in NoneType.

So, I want to build a logic where my script checks if the value returned is Null and extract the data only if the value is not Null.

How can I do this?

May 9, 2019 in Python by Shaan
16,150 views

1 answer to this question.

+1 vote

You can add an if condition to check and then extract the date if there is any value returned. Something like this:

if (name=page.find('a',{'class':'item-name'}):
    name=page.find('a',{'class':'item-name'}).getText()


Hope this helps!!

If you need to learn more about Python, It's recommended to join Python Training today.

Thanks!

answered May 9, 2019 by Ruby

Related Questions In Python

+1 vote
1 answer

How to check if a string is null in python

Try this: if cookie and not cookie.isspace(): # the ...READ MORE

answered Aug 20, 2018 in Python by Priyaj
• 58,020 points
23,146 views
0 votes
1 answer

Python Null Object

Use the 'None' keyword to denote the ...READ MORE

answered Jul 20, 2018 in Python by Nietzsche's daemon
• 4,260 points
865 views
0 votes
1 answer

How to find if setuptools is installed in python?

You can use python interpreter to check ...READ MORE

answered Jan 29, 2019 in Python by Omkar
• 69,220 points
2,281 views
0 votes
1 answer

How do I check if input string is a valid regular expression or not in Python?

Hi. Good question! Well, just like what ...READ MORE

answered Feb 12, 2019 in Python by Nymeria
• 3,560 points
11,315 views
0 votes
1 answer

How to web scrape using python without using a browser?

Yes, you can use the headless mode. ...READ MORE

answered Apr 2, 2019 in Python by Yogi

edited Oct 7, 2021 by Sarfaraz 13,109 views
0 votes
1 answer

How to parse html file to BeautifulSoup?

Hey. Refer to the following code: driver.get("link") html = ...READ MORE

answered Apr 2, 2019 in Python by Kirti
1,976 views
0 votes
1 answer
0 votes
1 answer

GAE: python code to check if i'm on dev_appserver or deployed to appspot

I don't know if you still need ...READ MORE

answered Sep 11, 2018 in Python by anonymous
919 views
0 votes
1 answer

How to check if a substring is present in a string using Python?

To check if the substring exists in ...READ MORE

answered May 9, 2019 in Python by Sharan
1,169 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP