首页 > 解决方案 > 我可以使用 selenium.webdriver.Chrome 从 ASDA 网站提取产品信息吗?

问题描述

我正在尝试在 Python 中使用 selenium.webdriver.chrome 从 groceries.asda.com 中提取产品信息

但是,如果我转到特定页面(https://groceries.asda.com/shelf/everyday-family-cereals/2-for-4-cereals/_/3097456947)并在 Google Chrome 中查看其源代码,我可以'找不到产品名称,所以我无法使用 find_element_by_xpath 等方法

有谁知道如何使用 selenium 从 ASDA 网站上提取产品名称?

试过xpath、css选择器,都返回NoSuchElementException

from selenium import webdriver

url = "https://groceries.asda.com/shelf/everyday-family-cereals/2-for-4-cereals/_/3097456947"

driver = webdriver.Chrome()
driver.get(url)

nav = driver.find_element_by_xpath('//*[@id="listingsContainer-0bd93fc4-888f-52c5-1c47-5679e4aab507"]/div/div[1]/div[2]/div/div/div[2]/span[1]/a/span')

product = nav.text

print(product)

NoSuchElementException

标签: pythonseleniumjupyter-notebookjupyter

解决方案


尝试这种方法 - 这应该使用其“alt”属性打印所有产品详细信息

Products = driver.find_elements_by_xpath("//div[@class=' co-product-list']//a/img")

print(len(Products))
for product in Products:
    print(product.get_attribute('alt'))

推荐阅读