Dec-04-2022, 12:06 AM
Hello,
I'm trying to test this code that checks the price of an Amazon product, but when I try running it, I get this error:
Thanks in advance.
Code:
I'm trying to test this code that checks the price of an Amazon product, but when I try running it, I get this error:
Error:line 31, in checking_price
product_title = soup.find('span', id='productTitle').getText()
AttributeError: 'NoneType' object has no attribute 'getText'
How do I fix this?Thanks in advance.
Code:
import requests #pip install requests from bs4 import BeautifulSoup #pip install bs4 import os import time import json # #Opening The Settings.json file # with open('settings.json','r') as file: # settings = json.load(file) # Set your budget # my_price = settings['budget'] my_price = 400 # initializing Currency Symbols to substract it from our string currency_symbols = ['€', ' £', '$', "¥", "HK$", "₹", "¥", "," ] # the URL we are going to use # URL = settings['url'] URL = 'https://www.amazon.ca/MSI-Geforce-192-bit-Support-Graphics/dp/B07ZHDZ1K6/ref=sr_1_16?crid=1M9LHOYX99CQW&keywords=Nvidia%2BGTX%2B1060&qid=1670109381&sprefix=nvidia%2Bgtx%2B1060%2Caps%2C79&sr=8-16&th=1' # Google "My User Agent" And Replace It headers = {"User-Agent": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36'} #Checking the price def checking_price(): page = requests.get(URL, headers=headers) soup = BeautifulSoup(page.text, 'html.parser') #Finding the elements product_title = soup.find('span', id='productTitle').getText() product_price = soup.find('span', class_ = "a-offscreen").getText() # using replace() to remove currency symbols for i in currency_symbols : product_price = product_price.replace(i, '') #Converting the string to integer product_price = int(float(product_price)) ProductTitleStrip = product_title.strip() print(ProductTitleStrip) print(product_price) # checking the price if(product_price<my_price): print("You Can Buy This Now!") else: print("The Price Is Too High!") checking_price()