
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