我正在嘗試從此頁面獲取所有畫廊影像 url 。每個產品都有多個影像。這是我嘗試獲取影像網址的代碼:
img = driver.find_elements_by_css_selector('.gem-gallery-thumbs-carousel img')
for i in img:
y = i.get_attribute('href')
print(y)
result:
None
None
None
None
None
None
uj5u.com熱心網友回復:
- 這些元素中的影像 url 包含在
src屬性中,而不是href在評論中提到的。 - 您可能應該添加等待/延遲以在讀取元素內容之前加載頁面。
所以,這樣的事情經過適當的等待/延遲應該可以作業:
imgs = driver.find_elements_by_css_selector('.gem-gallery-thumbs-carousel img')
for img in imgs:
url = img.get_attribute('src')
print(url)
uj5u.com熱心網友回復:
要為那些需要 .get_attribute() 的 src 輸出 src
wait=WebDriverWait(driver, 60)
driver.get('https://ca.morilee.com/product/quinceanera-dresses/vizcaya/iridescent-crystal-beaded-quinceanera-dress/')
imgs=wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,".gem-gallery-thumbs-carousel img")))
for img in imgs:
print(img.get_attribute("src")
進口:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
輸出:
https://ca.morilee.com/wp-content/uploads/sites/10/2021/11/89331-0259-scaled-1-280x400.jpg
https://ca.morilee.com/wp-content/uploads/sites/10/2021/11/89331-0292-scaled-1-280x400.jpg
https://ca.morilee.com/wp-content/uploads/sites/10/2021/11/89331-0061-scaled-1-280x400.jpg
https://ca.morilee.com/wp-content/uploads/sites/10/2021/11/89331-0083-scaled-1-280x400.jpg
https://ca.morilee.com/wp-content/uploads/sites/10/2021/11/89331-0101-scaled-1-280x400.jpg
https://ca.morilee.com/wp-content/uploads/sites/10/2021/11/89331-0196-scaled-1-280x400.jpg
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/378048.html
下一篇:從字符中求和字典中的值
