Home > OS >  How to check if a element/tag exists on page
How to check if a element/tag exists on page

Time:06-04

I am trying to check if a element exists on the webpage with class name of "message message-information", Except when I try I just get a AttributeError: 'NoneType'.

base_name = "GeForce RTX 3070 Ti"
url = "https://www.evga.com/products/product.aspx?pn=08G-P5-3797-KL"

headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'}

site = requests.get(url, headers=headers)
soup = BeautifulSoup(site.content, 'html.parser')

stock_info = soup.find(class_="message message-information").get_text() # The ID of the stock element
   
# Check if the element is on the page, if not then say there is stock
if stock_info is None:
    # If the element exists
    tools.nogpustock(base_name)
else:
    # if the element does not exist
    tools.hasgpustock(base_name)

CodePudding user response:

just do not call the .get_text(), if the tag doesn't exist the value returned by soup.find(class_="message message-information") will be None and calling None.get_text() will trigger the AttributeError: 'NoneType'

your code might be like

base_name = "GeForce RTX 3070 Ti"
url = "https://www.evga.com/products/product.aspx?pn=08G-P5-3797-KL"

headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'}

site = requests.get(url, headers=headers)
soup = BeautifulSoup(site.content, 'html.parser')

stock_info_tag = soup.find(class_="message message-information") # The ID of the stock element
   
# Check if the element is on the page, if not then say there is stock
if stock_info is None:
    # If the element exists
    tools.nogpustock(base_name)
else:
    # if the element does not exist
    tools.hasgpustock(base_name)
    stock_info = stock_info_tag.get_text()
  • Related