【发布时间】:2020-06-25 15:55:26
【问题描述】:
我正在尝试从this 链接中提取有关打折产品的信息。目前,正在使用此代码在列表中返回结果:
from selenium import webdriver #need to be able to quit out of the brower
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
from bs4 import BeautifulSoup
opts = Options()
opts.headless=True
assert opts.headless # Operating in headless mode
browser = Firefox(options=opts)
browser.get(https://www.macys.com/shop/mens-clothing/mens-blazers-sports-coats/Productsperpage/120?id=16499)
html = browser.page_source
soup = 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')
如果产品未在销售,我如何返回列表中的元素,如“N/A”或“Not on Sale”?肯定会是某种 if/else 语句,但我不确定条件是什么。
感谢您的帮助,非常感谢!
【问题讨论】:
标签: python web-scraping beautifulsoup