首先,抱歉我的英語不好。實際上,我制作了一個在亞馬遜網頁上查找資料的腳本。我需要在亞馬遜網頁上使用 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高亮顯示):

有人知道如何用硒在python中刮這個ASIN嗎?求幫助!
uj5u.com熱心網友回復:
您可以等待并查找位于該標簽旁邊的跨度。
wait=WebDriverWait(driver, 60)
driver.get('https://www.amazon.fr/PlayStation-Édition-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-Édition-Standard-DualSense-Couleur/dp/ B08H93ZRK9
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/377644.html
