You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询使用Selenium从Etsy下载图片的方法及解决requests请求图片时的SSL错误问题

咨询使用Selenium从Etsy下载图片的方法及解决requests请求图片时的SSL错误问题

看起来你在用requests请求Etsy图片时碰到了SSL连接错误,我来给你分享几个解决思路,同时也会提供用Selenium下载图片的靠谱方案~

一、解决requests请求的SSL错误

你遇到的SSLEOFError通常是服务器SSL配置、请求头不完整,或者本地CA证书问题导致的,可以试试这几个方法:

  • 临时跳过SSL验证(仅测试用,有安全风险)
    在requests.get()里加上verify=False参数,同时可以关掉对应的警告:

    import requests
    import urllib3
    
    # 关闭不安全请求的警告
    urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
    
    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.36"
    }
    # 加入verify=False跳过SSL验证
    r = requests.get(URL_input, headers=headers, stream=True, verify=False)
    
    # 保存图片到本地
    with open("etsy_image.jpg", "wb") as f:
        for chunk in r.iter_content(chunk_size=1024):
            f.write(chunk)
    
  • 更新CA证书
    旧的本地CA证书可能导致验证失败,执行下面的命令更新certifi库:

    pip install --upgrade certifi
    
  • 补充完整请求头
    Etsy可能会检查请求的来源,给headers加上Referer字段试试:

    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.36",
        "Referer": "https://www.etsy.com/"
    }
    

二、用Selenium下载Etsy图片(更稳定)

Selenium模拟真实浏览器的会话,能更好地绕过反爬机制,不容易出现SSL或拦截问题,步骤如下:

  1. 先安装Selenium和对应浏览器的驱动(比如ChromeDriver,要和你的Chrome版本匹配)
  2. 参考代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import requests

# 配置Chrome选项,无头模式可以让浏览器在后台运行
chrome_options = Options()
chrome_options.add_argument("--headless=new")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument(f'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36')

driver = webdriver.Chrome(options=chrome_options)
try:
    # 先访问Etsy主站,建立合法会话
    driver.get("https://www.etsy.com/")
    img_url = "https://i.etsystatic.com/40895858/r/il/764bf3/4699592436/il_340x270.4699592436_edpm.jpg"
    
    # 获取浏览器的cookies,带上cookies请求图片更安全
    cookies = driver.get_cookies()
    cookie_dict = {cookie['name']: cookie['value'] for cookie in cookies}
    
    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.36",
        "Referer": "https://www.etsy.com/"
    }
    
    # 请求并保存图片
    r = requests.get(img_url, headers=headers, cookies=cookie_dict, stream=True)
    with open("etsy_image_selenium.jpg", "wb") as f:
        for chunk in r.iter_content(chunk_size=1024):
            f.write(chunk)
    print("图片下载成功啦!")
finally:
    # 记得关闭浏览器
    driver.quit()

备注:内容来源于stack exchange,提问作者mySun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 13:04:29