如何从etsy下载带有selenium的图像?

问题描述 投票:0回答:1

我需要从 etsy 下载图像

但是运行此代码后,返回此错误:

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)'))) 

我的代码是:

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)

如何解决这个问题?

python web-scraping selenium-chromedriver
1个回答
0
投票

您的代码在我的测试中运行正常,没有发出任何错误。您可以尝试使用此命令更新请求吗:

pip install --upgrade requests certifi

或尝试:

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

或者试试这个:

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}")

另一种方法:

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}")

或者请使用硒:

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()  # Use the appropriate WebDriver for your browser


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()

所有脚本都在我的测试中运行。谢谢你。

© www.soinside.com 2019 - 2024. All rights reserved.