我是否可以使用selenium.webdriver.Chrome从ASDA网站提取产品信息?

2024-09-28 19:24:02 发布

您现在位置:Python中文网/ 问答频道 /正文

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

但是如果我转到一个特定的页面(https://groceries.asda.com/shelf/everyday-family-cereals/2-for-4-cereals/_/3097456947)并在googlechrome中查看它的源代码,我就找不到产品名称,所以我无法使用find\u element\u by \u 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)

无接触异常


Tags: httpsdivcomfordriverseleniumfamilyxpath
2条回答

尝试这种方法-这应该使用“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'))

谢谢你的灵感。这是按计划进行的。谢谢你

Products = driver.find_elements_by_xpath("//div[@class='product-content']//span//a")

for product in Products:
    print(product.get_attribute('title'))

相关问题 更多 >