Google Images API:如何擷取圖片搜尋結果
了解如何使用 SERP API 擷取 Google Images 搜尋結果,並用 Python 收集圖片標題、縮圖、圖片 URL、來源頁面、網域、排名位置,最後匯出為 CSV。
快速回答: 你可以透過 SERP API 發送圖片搜尋查詢,取得結構化圖片搜尋資料,整理圖片標題、縮圖、圖片 URL、來源頁面、網域和排名位置,最後匯出為 CSV。
Google Images 資料很適合用於依賴視覺搜尋結果的工作流,而不只是一般網頁搜尋。
例如,你可能想了解:
-
某個查詢下出現哪些商品圖片
-
哪些網域在圖片搜尋中更常出現
-
競品使用了哪些縮圖
-
品牌搜尋下會出現哪些圖片
-
圖片搜尋結果是否會因國家或語言不同而變化
-
哪些視覺素材可以作為內容研究參考
我們要建立什麼?
我們會建立一個 Python 流程:
圖片搜尋查詢
→ SERP API 請求
→ 提取圖片結果
→ 整理圖片欄位
→ 匯出為 CSV
最終 CSV 可以包含:
|
欄位 |
含義 |
|
|
圖片搜尋查詢 |
|
|
結果位置 |
|
|
圖片結果標題 |
|
|
原圖 URL,如果可用 |
|
|
縮圖 URL |
|
|
圖片所在頁面 |
|
|
來源頁面網域 |
|
|
圖片尺寸,如果可用 |
|
|
採集時間 |
這套流程適合視覺 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_images、images 或 tbm=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
常用參數
|
參數 |
為什麼重要 |
|
|
圖片搜尋查詢 |
|
|
目標地區 |
|
|
國家或市場 |
|
|
搜尋語言 |
|
|
桌面或行動裝置結果差異 |
|
|
返回圖片結果數量 |
|
|
API 支援時可收集更多圖片結果頁 |
正式流程中,每一條結果都應保存地區、國家、語言、裝置和採集時間。圖片搜尋結果會隨時間和市場變化。
常見使用場景
視覺 SEO 研究
追蹤重要查詢下出現哪些圖片和來源頁面。
品牌監控
檢查品牌名、產品名、人物、活動或事件在圖片搜尋中呈現出的內容。
電商圖片研究
分析商品圖片風格、來源網域、縮圖和競品可見度。
內容研究
用圖片搜尋結果理解某個主題在網路上通常如何被視覺化呈現。
AI 和 RAG 工作流
把圖片搜尋結果作為資料發現層。系統可以先收集圖片 URL 和來源頁面,再判斷哪些來源需要深入處理。
FAQ
什麼是 Google Images API?
Google Images API 是以程式化方式收集 Google Images 類型搜尋結果的方法。本文中,它指透過 SERP API 返回結構化圖片搜尋資料。
可以用 Python 擷取 Google Images 結果嗎?
可以。你可以用 Python 發送圖片搜尋查詢到 SERP API,解析結構化回應,並把圖片結果欄位匯出為 CSV。
可以從圖片搜尋結果收集哪些欄位?
常見欄位包括標題、圖片 URL、縮圖 URL、來源頁面、來源網域、排名位置、寬度、高度和採集時間。
可以直接下載結果中的圖片嗎?
技術上,如果圖片 URL 可用,你可以請求該 URL。但下載或重用圖片前,應檢查版權、授權和使用條件。
圖片搜尋追蹤用 CSV 夠嗎?
小型測試和簡單報告可以使用 CSV。如果要做定期追蹤、去重、看板和歷史比較,建議使用資料庫。