0

I'm trying to extract all the pictures available on this website page with Python and Selenium

For this, i'm using the CSS selector, with the following code :

try :
    Pictures = self.browser.find_elements_by_css_selector('background-image')
except :
    Pictures = None

for picture in Pictures :
    print(picture.get_attribute("textContent"))

However, nothing happens : no message error, no blanks... just nothing.

My final target is to be able to download these pictures ; how can I do ?

Edit 1 :

enter image description here

2 Answers 2

1

Try the following to get the images from that page. Turn out that they are available in the page source, so you don't need to make use of selenium to grab the images. You can use requests module which is way faster than selenium. Given that this is how you can go:

import re
import requests
from bs4 import BeautifulSoup

link = "https://www.sezane.com/fr/e-shop/collection-printemps-robes-robes-courtes"

res = requests.get(link)
soup = BeautifulSoup(res.text,"lxml")
for item in soup.select("[id='cms_sortable'] .free-product__img"):
    match_image = re.search(r"url\('(.*?)'",item.get("style"))
    if match_image:
        print(match_image.group(1))

Output are like:

https://media.sezane.com/image/upload/q_auto:best/v1586276035/website/cms/product/imh92y33wziscjtz1mta.jpg
https://media.sezane.com/image/upload/q_auto:best/v1585924567/website/cms/product/c7umxdbyyqocwc5gzghy.jpg
https://media.sezane.com/image/upload/q_auto:best/v1585924561/website/cms/product/jfdjdxqo5ek3oeziggxc.jpg
https://media.sezane.com/image/upload/q_auto:best/v1585931708/website/cms/product/qhmxfoo95ftlbzsiplid.jpg
https://media.sezane.com/image/upload/q_auto:best/v1585907671/website/cms/product/ng7vbbd39cnooqzqgzgt.jpg
Sign up to request clarification or add additional context in comments.

3 Comments

I will check Please can you explain me how you choose between to go with selenium or bs4 ?
Selenium is a browser simulator and bs4 is an HTML parser. I don't understand what do you mean by choosing between the two.
working very well, thanks. could you comment the loop of your code please ?
0

please try below solution, in your code csss selector was incorrect ::

 driver.maximize_window()
 driver.get('https://www.sezane.com/fr/e-shop/collection-printemps-robes-robes-courtes')   
 elements = WebDriverWait(driver, 15).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[contains(@id,'_divVisualPrimary')]")))

for element in elements:
   element=element.value_of_css_property("background-image")
   print(re.split('[()]',element)[1])

Note : please add below imports to your solution

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

enter image description here

3 Comments

"free product link" is the link to the detail page, not to the images no ?
Can you please elaborate more because there is no background-image present on the page with css selector
@AlexDana Please find working solution and once you verify would you kind enough to accept answer and hit upvote button from your end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.