简体   繁体   English

WebScraping w/ Beautiful Soup:如何返回列表中的空元素?

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

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 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')

If a product isn't on sale, how could I return an element in the list like "N/A" or "Not on Sale"?如果产品未在销售,我如何返回列表中的元素,如“N/A”或“Not on Sale”? Definitely will be some kind of if/else statement, but I'm not sure what the condition would be.肯定会是某种 if/else 语句,但我不确定条件是什么。

Any help is appreciated, thank you so much!任何帮助表示赞赏,非常感谢!

hi please share your full code brother.. this might help you in asking question.嗨,请分享您的完整代码兄弟..可能会帮助您提出问题。 try this replace soup.find_all(class_='discount') with soup.findAll("span", {"class": "discount"}) and check result and please update your question properly.试试这个用soup.findAll("span", {"class": "discount"})替换soup.find_all(class_='discount')并检查结果,请正确更新您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM