
API Key 密钥:深入理解与应用
organic_results
、knowledge_graph
、local_results
等字段,无需手动解析 HTML。tbm=isch
(图片)、nws
(新闻)、shop
、vid
(视频)等。location
、gl
、hl
参数模拟全球不同地区搜索环境。核心关键词:SerpAPI、Google 搜索结果、搜索爬虫、JSON 结构化数据、分页抓取、本地化搜索
pip install serpapi python-dotenv
import os
from dotenv import load_dotenv
from serpapi import GoogleSearch
load_dotenv()
params = {
"q": "coffee shops",
"engine": "google",
"location": "New York, United States",
"api_key": os.getenv("SERPAPI_KEY"),
"num": 100,
"start": 0,
"no_cache": True
}
search = GoogleSearch(params)
result = search.get_dict()
q
:搜索关键词engine
:搜索类型(google
、images
、nws
…)num
+ start
:分页抓取 参数,支持最多 num=100
no_cache
:禁用缓存,强制 实时拉取organic = result.get("organic_results", [])
for idx, item in enumerate(organic, 1):
print(f"{idx}. {item['title']} — {item['link']}")
print(f" 摘要:{item.get('snippet')}")
knowledge_graph
、local_results
、inline_images
等高级字段,拓展数据维度。location
,对比 SERP 差异。q
中的 site:
、inurl:
、filetype:
实现精确筛选。time.sleep
和重试机制,规避 API Rate Limit。output=json
,并启用 no_cache=false
重试。location
字符串和可选 gl
/hl
参数。通过本文,你已掌握使用 SerpAPI 获取 Google 搜索结果 的核心流程:
q
、location
、num
、start
、no_cache
)详解;立即将这些实战技巧应用于你的 搜索爬虫 和 市场情报 项目,实现更高效、可靠的数据采集与分析!
原文引自YouTube视频:https://www.youtube.com/watch?v=-H9yN_jl4FQ