Python BeautifulSoup check if find returns Null object

0 votes

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 in Python by Shaan
9 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

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()
answered May 9 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
• 56,120 points
779 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
20 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 in Python by Omkar
• 65,850 points
12 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 in Python by Nymeria
• 3,500 points
14 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 in Python by Kirti
12 views
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 10, 2018 in Python by anonymous
17 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 in Python by Sharan
6 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.