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

使用google_images_download下载图片失败的技术求助

解决google_images_download无法下载图片返回空列表的问题

我之前也踩过这个坑,核心原因是原google_images_download包已经很久没维护了,Google图片搜索的页面结构更新后,它的解析逻辑完全失效了。给你几个亲测有效的解决方案:

1. 安装社区维护的fork版本

原仓库早已停更,不少开发者fork后修复了Google页面解析的问题。先卸载旧版本,再安装这个活跃维护的分支:

pip uninstall google_images_download -y
pip install git+https://github.com/Joeclinton1/google-images-download.git

安装完成后重新运行你的代码,大概率能恢复正常下载。

2. 添加参数规避反爬与JS渲染限制

如果升级后还是不行,那可能是Google的反爬机制或JS渲染要求导致的,试试补充这些参数:

  • 先下载对应浏览器的驱动(比如Chrome的chromedriver),放到系统可执行路径下
  • 修改你的代码参数:
from google_images_download import google_images_download

response = google_images_download.googleimagesdownload()
arguments = {
    "keywords": "lilly,hills",
    "limit": 5,
    "print_urls": True,
    "chromedriver": "/usr/local/bin/chromedriver",  # 替换成你的驱动实际路径
    "delay": 2,  # 增加请求间隔避免触发反爬
    "no_directory": False  # 确保按关键词创建存储目录
}
paths = response.download(arguments)
print(paths)

3. 替换为更稳定的替代工具

如果上面的方法都不奏效,推荐换用更可靠的库,比如基于DuckDuckGo的search_images_ddg(无需API密钥,稳定性拉满):
先安装依赖:

pip install duckduckgo-search requests

然后用这段代码实现下载:

from duckduckgo_search import search_images_ddg
import requests
import os

# 配置下载参数
keywords = ["lilly", "hills"]
save_root = "google_images"
max_count = 5

# 创建存储目录
os.makedirs(save_root, exist_ok=True)

for keyword in keywords:
    print(f"开始下载关键词: {keyword}")
    # 搜索图片
    img_results = search_images_ddg(keyword, max_results=max_count)
    # 逐个下载保存
    for idx, result in enumerate(img_results):
        try:
            img_path = os.path.join(save_root, f"{keyword}_{idx+1}.jpg")
            with open(img_path, "wb") as f:
                f.write(requests.get(result["image"]).content)
            print(f"已保存: {img_path}")
        except Exception as e:
            print(f"下载失败 {result['image']}: {str(e)}")

4. 排查网络与反爬限制

  • 如果是公司/校园网络,可能存在防火墙拦截,试试切换到个人热点或VPN
  • 若频繁请求被Google屏蔽,可以在参数中添加代理:"proxy": "http://你的代理地址:端口"

内容的提问来源于stack exchange,提问作者user13897142

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 11:53:16