【问题标题】:WebScraping w/ Beautiful Soup: How to Return an Empty Element in a List?使用 Beautifulsoup 进行网页抓取:如何返回列表中的空元素?
【发布时间】: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


    【解决方案1】:

    嗨,请分享您的完整代码兄弟。this 可能会帮助您提出问题。 尝试将soup.find_all(class_='discount') 替换为soup.findAll("span", {"class": "discount"}) 并检查结果,请正确更新您的问题。

    【讨论】:

    • 非常感谢,我去试试!抱歉,编程/堆栈溢出的新手,感谢您的帮助!
    猜你喜欢
    • 2021-11-15
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    相关资源
    最近更新 更多