Google Images API:如何采集图片搜索结果

了解如何使用 SERP API 采集 Google Images 搜索结果,并用 Python 收集图片标题、缩略图、图片 URL、来源页面、域名、排名位置,最后导出为 CSV。

Google Images API:如何采集图片搜索结果
Ethan Caldwell
最后更新于
6 分钟阅读

快速回答: 你可以通过 SERP API 发送图片搜索查询,获取结构化图片搜索数据,整理图片标题、缩略图、图片 URL、来源页面、域名和排名位置,最后导出为 CSV。

Google Images 数据很适合用于依赖视觉搜索结果的工作流,而不只是普通网页搜索。

例如,你可能想了解:

  • 某个查询下出现哪些商品图片

  • 哪些域名在图片搜索中更常出现

  • 竞品使用了哪些缩略图

  • 品牌搜索下会出现哪些图片

  • 图片搜索结果是否会因国家或语言不同而变化

  • 哪些视觉素材可以作为内容研究参考

我们要构建什么?

我们会建立一个 Python 流程:

图片搜索查询
→ SERP API 请求
→ 提取图片结果
→ 整理图片字段
→ 导出为 CSV

最终 CSV 可以包含:

字段

含义

query

图片搜索查询

position

结果位置

title

图片结果标题

image_url

原图 URL,如果可用

thumbnail_url

缩略图 URL

source_page

图片所在页面

source_domain

来源页面域名

width / height

图片尺寸,如果可用

collected_at

采集时间

这套流程适合视觉 SEO、电商研究、品牌监控、竞品分析、内容研究和 AI 数据发现工作流。

为什么用 SERP API 采集图片搜索结果?

你可以尝试手动抓取 Google Images,但图片搜索页面通常是动态加载,比普通网页搜索结果更难解析。

常见问题包括:

  • 图片延迟加载

  • 缩略图 URL 和原图 URL 不一致

  • 页面版面变化

  • 重复图片结果

  • 来源页面跳转

  • 图片尺寸缺失

  • 地区和语言差异

  • 请求被限制或遇到验证码

SERP API 可以让流程更干净。你发送查询,然后获取可被程序稳定解析的结构化数据。

Step 1:设置环境变量

不要把 API 密钥直接写进代码。

export TALORDATA_API_KEY="your_api_key_here"
export TALORDATA_SERP_ENDPOINT="your_talordata_serp_endpoint_here"

Windows PowerShell:

$env:TALORDATA_API_KEY="your_api_key_here"
$env:TALORDATA_SERP_ENDPOINT="your_talordata_serp_endpoint_here"

安装 Python 依赖:

pip install requests

请使用后台或 API 文档中显示的端点和验证方式。

Step 2:建立请求辅助函数

把请求层和解析逻辑分开。这样后续端点或验证方式变化时,只需要修改一个函数。

import os
import requests


def require_env(name):
    value = os.getenv(name)
    if not value:
        raise RuntimeError(f"Missing required environment variable: {name}")
    return value


TALORDATA_API_KEY = require_env("TALORDATA_API_KEY")
TALORDATA_SERP_ENDPOINT = require_env("TALORDATA_SERP_ENDPOINT")


def call_serp_api(payload):
    headers = {
        "Authorization": f"Bearer {TALORDATA_API_KEY}",
        "Content-Type": "application/json",
    }

    response = requests.post(
        TALORDATA_SERP_ENDPOINT,
        json=payload,
        headers=headers,
        timeout=30,
    )

    response.raise_for_status()
    return response.json()

有些 API 使用 GET,也有些会把 API key 放在查询参数中。这些差异都可以放在辅助函数中处理。

Step 3:发送 Google Images 请求

建立一个图片搜索请求函数。

def search_google_images(query, location="United States", language="en", country="us", device="desktop"):
    payload = {
        "engine": "google_images",
        "q": query,
        "location": location,
        "hl": language,
        "gl": country,
        "device": device,
        "num": 20,
        "output": "json",
    }

    return call_serp_api(payload)

根据你的 API 设置,图片搜索类型可能使用 google_imagesimagestbm=isch 等写法。把请求函数独立出来,后续只需要改这一处。

Step 4:提取图片结果

图片结果可能出现在不同字段下,例如:

images_results
image_results
images

下面的解析器支持几种常见响应结构。

from datetime import datetime, timezone
from urllib.parse import urlparse


def clean_text(value):
    if not value:
        return ""
    return " ".join(str(value).split())


def get_domain(url):
    if not url:
        return ""

    parsed = urlparse(url)
    return parsed.netloc.replace("www.", "")


def get_image_results(serp_json):
    return (
        serp_json.get("images_results")
        or serp_json.get("image_results")
        or serp_json.get("images")
        or []
    )


def normalize_image_results(serp_json, query):
    results = get_image_results(serp_json)
    collected_at = datetime.now(timezone.utc).isoformat()

    rows = []

    for index, item in enumerate(results, start=1):
        image_url = (
            item.get("original")
            or item.get("image")
            or item.get("image_url")
            or item.get("link")
            or ""
        )

        thumbnail_url = (
            item.get("thumbnail")
            or item.get("thumbnail_url")
            or item.get("thumbnail_link")
            or ""
        )

        source_page = (
            item.get("source")
            or item.get("source_page")
            or item.get("page_url")
            or item.get("link")
            or ""
        )

        rows.append({
            "query": query,
            "position": item.get("position") or item.get("rank") or index,
            "title": clean_text(item.get("title")),
            "image_url": image_url,
            "thumbnail_url": thumbnail_url,
            "source_page": source_page,
            "source_domain": get_domain(source_page),
            "width": item.get("width") or item.get("image_width") or "",
            "height": item.get("height") or item.get("image_height") or "",
            "collected_at": collected_at,
        })

    return rows

整理后的结构更适合放入表格、数据看板或数据库。

Step 5:导出图片结果到 CSV

import csv


CSV_COLUMNS = [
    "query",
    "position",
    "title",
    "image_url",
    "thumbnail_url",
    "source_page",
    "source_domain",
    "width",
    "height",
    "collected_at",
]


def export_to_csv(rows, filename="google_images_results.csv"):
    if not rows:
        print("No image results found.")
        return

    with open(filename, mode="w", newline="", encoding="utf-8") as file:
        writer = csv.DictWriter(file, fieldnames=CSV_COLUMNS)
        writer.writeheader()
        writer.writerows(rows)

    print(f"Exported {len(rows)} image results to {filename}")

完整 Python 示例

import os
import csv
import requests

from datetime import datetime, timezone
from urllib.parse import urlparse


CSV_COLUMNS = [
    "query",
    "position",
    "title",
    "image_url",
    "thumbnail_url",
    "source_page",
    "source_domain",
    "width",
    "height",
    "collected_at",
]


def require_env(name):
    value = os.getenv(name)
    if not value:
        raise RuntimeError(f"Missing required environment variable: {name}")
    return value


TALORDATA_API_KEY = require_env("TALORDATA_API_KEY")
TALORDATA_SERP_ENDPOINT = require_env("TALORDATA_SERP_ENDPOINT")


def call_serp_api(payload):
    headers = {
        "Authorization": f"Bearer {TALORDATA_API_KEY}",
        "Content-Type": "application/json",
    }

    response = requests.post(
        TALORDATA_SERP_ENDPOINT,
        json=payload,
        headers=headers,
        timeout=30,
    )

    response.raise_for_status()
    return response.json()


def search_google_images(query, location="United States", language="en", country="us", device="desktop"):
    payload = {
        "engine": "google_images",
        "q": query,
        "location": location,
        "hl": language,
        "gl": country,
        "device": device,
        "num": 20,
        "output": "json",
    }

    return call_serp_api(payload)


def clean_text(value):
    if not value:
        return ""
    return " ".join(str(value).split())


def get_domain(url):
    if not url:
        return ""

    parsed = urlparse(url)
    return parsed.netloc.replace("www.", "")


def get_image_results(serp_json):
    return (
        serp_json.get("images_results")
        or serp_json.get("image_results")
        or serp_json.get("images")
        or []
    )


def normalize_image_results(serp_json, query):
    results = get_image_results(serp_json)
    collected_at = datetime.now(timezone.utc).isoformat()

    rows = []

    for index, item in enumerate(results, start=1):
        image_url = (
            item.get("original")
            or item.get("image")
            or item.get("image_url")
            or item.get("link")
            or ""
        )

        thumbnail_url = (
            item.get("thumbnail")
            or item.get("thumbnail_url")
            or item.get("thumbnail_link")
            or ""
        )

        source_page = (
            item.get("source")
            or item.get("source_page")
            or item.get("page_url")
            or item.get("link")
            or ""
        )

        rows.append({
            "query": query,
            "position": item.get("position") or item.get("rank") or index,
            "title": clean_text(item.get("title")),
            "image_url": image_url,
            "thumbnail_url": thumbnail_url,
            "source_page": source_page,
            "source_domain": get_domain(source_page),
            "width": item.get("width") or item.get("image_width") or "",
            "height": item.get("height") or item.get("image_height") or "",
            "collected_at": collected_at,
        })

    return rows


def export_to_csv(rows, filename="google_images_results.csv"):
    if not rows:
        print("No image results found.")
        return

    with open(filename, mode="w", newline="", encoding="utf-8") as file:
        writer = csv.DictWriter(file, fieldnames=CSV_COLUMNS)
        writer.writeheader()
        writer.writerows(rows)

    print(f"Exported {len(rows)} image results to {filename}")


if __name__ == "__main__":
    query = "modern office desk setup"

    serp_json = search_google_images(
        query=query,
        location="United States",
        language="en",
        country="us",
        device="desktop",
    )

    rows = normalize_image_results(serp_json, query)
    export_to_csv(rows)

执行后会得到:

google_images_results.csv

输出示例

query,position,title,image_url,thumbnail_url,source_page,source_domain,width,height,collected_at
modern office desk setup,1,Example Desk Setup,https://example.com/image.jpg,https://example.com/thumb.jpg,https://example.com/blog/desk-setup,example.com,1200,800,2026-06-22T00:00:00Z
modern office desk setup,2,Minimal Office Setup,https://example.org/photo.jpg,https://example.org/thumb.jpg,https://example.org/office-ideas,example.org,1600,900,2026-06-22T00:00:00Z

常用参数

参数

为什么重要

q

图片搜索查询

location

目标地区

gl

国家或市场

hl

搜索语言

device

桌面或移动设备结果差异

num

返回图片结果数量

ijn / 分页

API 支持时可采集更多图片结果页

正式流程中,每一条结果都应保存地区、国家、语言、设备和采集时间。图片搜索结果会随时间和市场变化。

常见使用场景

视觉 SEO 研究

追踪重要查询下出现哪些图片和来源页面。

品牌监控

检查品牌名、产品名、人物、活动或事件在图片搜索中呈现出的内容。

电商图片研究

分析商品图片风格、来源域名、缩略图和竞品可见度。

内容研究

用图片搜索结果理解某个主题在网络上通常如何被视觉化呈现。

AI 和 RAG 工作流

把图片搜索结果作为数据发现层。系统可以先采集图片 URL 和来源页面,再判断哪些来源需要深入处理。
免费测试Google Images API>>

FAQ

什么是 Google Images API?

Google Images API 是以程序化方式采集 Google Images 类型搜索结果的方法。本文中,它指通过 SERP API 返回结构化图片搜索数据。

可以用 Python 采集 Google Images 结果吗?

可以。你可以用 Python 发送图片搜索查询到 SERP API,解析结构化响应,并把图片结果字段导出为 CSV。

可以从图片搜索结果采集哪些字段?

常见字段包括标题、图片 URL、缩略图 URL、来源页面、来源域名、排名位置、宽度、高度和采集时间。

可以直接下载结果中的图片吗?

技术上,如果图片 URL 可用,你可以请求该 URL。但下载或重用图片前,应检查版权、授权和使用条件。

图片搜索追踪用 CSV 够吗?

小型测试和简单报告可以使用 CSV。如果要做定期追踪、去重、看板和历史比较,建议使用数据库。

立即开展您的数据业务

加入全球最强大的代理网络

免费试用