【问题标题】:selenium python why I am not getting image url?selenium python 为什么我没有得到图片网址?
【发布时间】:2022-01-14 16:01:03
【问题描述】:

我正在尝试从此page 获取所有图库图片网址。每个产品都有多个图像。这是我试图获取图片网址的代码:

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

【问题讨论】:

  • 图像通常具有src 属性,而不是href。特别是在那个页面上,它们看起来有一个data-tgpli-src 属性。
  • 约翰·戈登我知道并尝试过,但没有成功
  • 更新问题以包含您期望找到的元素的示例。
  • 约翰戈登谢谢。在将 href 更改为 src 后,它现在可以工作了,但不明白为什么它以前不能工作。顺便谢谢。

标签: python python-3.x selenium selenium-webdriver web-scraping


【解决方案1】:
  1. 这些元素中的图像 URL 包含在 src 属性中,而不是在 cmets 中提到的 href 中。
  2. 您可能应该添加等待/延迟以在读取元素内容之前加载页面。
    所以,像这样After适当的等待/延迟应该可以工作:
imgs = driver.find_elements_by_css_selector('.gem-gallery-thumbs-carousel img')
for img in imgs:
    url = img.get_attribute('src')
    print(url)

【讨论】:

    【解决方案2】:

    要为那些你需要 .get_attribute() for 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
    

    【讨论】:

      猜你喜欢
      • 2020-07-16
      • 2022-01-22
      • 2023-03-17
      • 2020-11-19
      • 2013-07-15
      • 1970-01-01
      • 2020-04-09
      • 2015-06-03
      • 1970-01-01
      相关资源
      最近更新 更多