How to download product images, with selenium in python

Viewed 44

I am not able to download the product images.

The code line to download the image I reused from another project, but in this case it is not running as it should, because the code does not find the "src" element.

I need to download the first and second product image.

The file name can be anything (image01, image02...)

import pandas as pd
import xlsxwriter
import urllib.request
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ChromeOptions, Chrome
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager 


opts = ChromeOptions()
opts.add_experimental_option("detach", True)
servico=Service(ChromeDriverManager().install())
driver=webdriver.Chrome(service=servico, options=opts)

driver.get("https://pt.aliexpress.com/item/32822452433.html?algo_pvid=3e115ade-85a0-4843-9915-db4524a88b37&algo_exp_id=3e115ade-85a0-4843-9915-db4524a88b37-2&pdp_ext_f=%7B%22sku_id%22%3A%2210000000005806972%22%7D&pdp_npi=2%40dis%21BRL%2152.5%2135.68%21%21%21%21%21%402101e9d216636294181888642ea103%2110000000005806972%21sea&curPageLogUid=s0iQJCGqX5wS")
driver.implicitly_wait(2) # seconds
driver.maximize_window()


 nomeProduto = driver.find_element(By.CLASS_NAME,  "product-title-text").text
 frete = driver.find_element(By.CSS_SELECTOR, "#root > div > div.product-main > div > div.product-info > div.product-dynamic-shipping > div > div > div.dynamic-shipping-line.dynamic-shipping-titleLayout > span > span > strong").text      
 try:
    valor = driver.find_element(By.CSS_SELECTOR, "#root > div > div.product-main > div > div.product-info > div.product-price > div.product-price-current > span").text
 except:
    valor = driver.find_element(By.CSS_SELECTOR, "#root > div > div.product-main > div > div.product-info > div.uniform-banner > div.uniform-banner-box > div:nth-child(1) > span.uniform-banner-box-price").text

    divImagens = driver.find_elements(By. CLASS_NAME, "image-view-magnifier-wrap")[0]
    primeiraImagem = divImagens.find_element(By. TAG_NAME, "img")
    atributoSrc = primeiraImagem.get_attribute("src")
    print(atributoSrc)

    try:
      urllib.request.urlretrieve(atributoSrc,r"C:\Users\felip\OneDrive\Área de Trabalho\Web\VS\Imagens\nome.jpg")

      print("Download OK")
    except:
      print("Error")


 print(nomeProduto)
 print(valor)
 print(frete)
1 Answers
Related