Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

WebScraping w/ Beautiful Soup: How to Return an Empty Element in a List?

$
0
0

I am trying to pull information on discounted products from this link. Currently, the results are being returned in a list using this code:

from selenium import webdriver #need to be able to quit out of the browerfrom selenium.webdriver import Firefoxfrom selenium.webdriver.firefox.options import Optionsfrom bs4 import BeautifulSoupopts = Options()opts.headless=Trueassert opts.headless  # Operating in headless modebrowser = Firefox(options=opts)browser.get(https://www.macys.com/shop/mens-clothing/mens-blazers-sports-coats/Productsperpage/120?id=16499)html = browser.page_sourcesoup = BeautifulSoup(html,'html.parser')discount = []for tag in soup.find_all(class_='discount'):   discount.append(tag.text.strip())print(discount)browser.quit()print('The program is terminated')

If a product isn't on sale, how could I return an element in the list like "N/A" or "Not on Sale"? Definitely will be some kind of if/else statement, but I'm not sure what the condition would be.

Any help is appreciated, thank you so much!


Viewing all articles
Browse latest Browse all 12111

Trending Articles