pythonweb-scrapingselenium-chromedriver

How to download image with selenium from etsy?


I need dwonload image from etsy

But after run this code, return this error:

requests.exceptions.SSLError: HTTPSConnectionPool(host='i.etsystatic.com', port=443): Max retries exceeded with url: /40895858/r/il/764bf3/4699592436/il_340x270.4699592436_edpm.jpg (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1145)')))

My code is:

URL_input =  "https://i.etsystatic.com/40895858/r/il/764bf3/4699592436/il_340x270.4699592436_edpm.jpg"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.3"
    }
    r = requests.get(URL_input, headers=headers, stream=True)

How to issue this problem?


Solution

  • Your code is working in my testing without issuing any error. Could you try updating requests using this command:

    pip install --upgrade requests certifi
    

    or try:

    response = requests.get(url, verify=False)
    

    Or try this:

    import requests
    from requests.adapters import HTTPAdapter
    from urllib3.util.ssl_ import create_urllib3_context
    
    class TLSAdapter(HTTPAdapter):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.ssl_context = create_urllib3_context()
    
    session = requests.Session()
    session.mount("https://", TLSAdapter())
    
    url = "https://i.etsystatic.com/40895858/r/il/764bf3/4699592436/il_340x270.4699592436_edpm.jpg"
    response = session.get(url)
    
    if response.status_code == 200:
        with open("downloaded_image1.jpg", "wb") as file:
            file.write(response.content)
        print("Image downloaded successfully!")
    else:
        print(f"Failed to download image. Status code: {response.status_code}")
    

    Another approach:

    import requests
    
    url = "https://i.etsystatic.com/40895858/r/il/764bf3/4699592436/il_340x270.4699592436_edpm.jpg"
    
    file_path = "downloaded_image.jpg"
    
    try:
        response = requests.get(url, stream=True)
        if response.status_code == 200:
             with open(file_path, "wb") as file:
                 for chunk in response.iter_content(1024):
                     file.write(chunk)
             print(f"Image successfully downloaded and saved to {file_path}")
        else:
             print(f"Failed to download image. Status code: {response.status_code}")
    except Exception as e:
        print(f"An error occurred: {e}")
    

    If none of the above scripts works, please try "import time" after "import requests", and before "response = requests.get ..." use "time.sleep(0.01)".

    Or using selenium please:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    driver = webdriver.Chrome()
    
    
    image_url = "https://i.etsystatic.com/40895858/r/il/764bf3/4699592436/il_340x270.4699592436_edpm.jpg"
    driver.get(image_url)
    
    image_element = driver.find_element(By.TAG_NAME, "img")
    
    image_data = image_element.screenshot_as_png
    
    file_path = "downloaded_image_s.jpg"
    with open(file_path, "wb") as file:
        file.write(image_data)
    
    print(f"Image successfully downloaded and saved to {file_path}")
    
    driver.quit()
    

    All scripts are working in my testing. Thank you.