在亚马逊网页产品上抓取 ASIN

Scrape ASIN on amazon webpage product

首先,抱歉我的英语不好。 实际上,我制作了一个在亚马逊网页上查找数据的脚本。我需要在亚马逊网页上使用 python 和 selenium 抓取 asin。 我已经制作了这段代码来抓取 asin:

    firstResult = driver.find_element_by_css_selector('div[data-index="1"]>div')
    asin = firstResult.get_attribute('data-asin')

但是它不起作用,我的结果有一些错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"div[data-index="1"]>div"}
  (Session info: headless chrome=96.0.4664.45)

网页上有asin编号的源码部分(ASIN高亮显示):

有人知道如何用 selenium 在 python 中抓取这个 ASIN 吗? 求助!

您可以等待并查找位于该标签旁边的跨度。

wait=WebDriverWait(driver, 60)
driver.get('https://www.amazon.fr/PlayStation-%C3%89dition-Standard-DualSense-Couleur/dp/B08H93ZRK9')
elem=wait.until(EC.presence_of_element_located((By.XPATH," //span[@class='a-list-item' and contains (.,'ASIN')]//span[2]")))
print(elem.text)

进口:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

输出:

B08H93ZRK9

另一件事是 url 实际上在最后具有相同的值。 对driver.current_url,

进行简单的字符串操作即可得到

https://www.amazon.fr/PlayStation-%C3%89dition-Standard-DualSense-Couleur/dp/ B08H93ZRK9