Need help to resolve this - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Need help to resolve this (/thread-25359.html) |
Need help to resolve this - MK2020 - Mar-28-2020 I'm new to Phyton and do not have any Programming background. I downloaded Phyton 3.8 and PYcharm. I tried to work the program below and am getting the below error AttributeError: 'NoneType' object has no attribute 'get_text' Looks like no values are returned by soup.find statement, how to proceed further? Thank you import requests from bs4 import BeautifulSoup URL = 'https://www.amazon.com/Disposable-Offices-Households-Sensitive-Crowded/dp/B085TF7JPK/ref=sr_1_1?dchild=1&keywords=masks&qid=1585366634&sr=8-1' headers = {"User-Agent" : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36' } page = requests.get(URL , headers=headers) soup = BeautifulSoup(page.content, "html.parser") print(soup.prettify()) title = soup.find(id = "productTitle" ).get_text() price = soup.find(id = "priceblock_ourprice" ).get_text() print(title) print(price) RE: Need help to resolve this - SheeppOSU - Mar-28-2020 I don't know anything about working with requests and BeautifulSoup, but, I would say to put the statements in a try except statement like this. try: title = soup.find(id = "productTitle" ).get_text() except: print("Title not found") try: price = soup.find(id = "priceblock_ourprice" ).get_text() except: print("Title not found")If you are having to iterate that over and over then I would suggest making it a function def FindById(ID): try: return soup.find(id = ID).get_text() except: print("Error: could not find") title = FindById("productTitle") price = FindById("priceblock_ourprice")Hopefully this helps |