Google包含有价值的数据 -- organic results, knowledge graph, People Also Ask, AI overview等。直接抓取这些数据意味着要应对反爬检测、CAPTCHA、IP轮换和不断变化的选择器。Scavio API处理所有这些问题,通过单次POST请求返回干净的结构化JSON。
本教程展示如何使用Python和Scavio API抓取Google。完成后,您将拥有一个可运行的Python脚本,用于获取实时Google数据并解析结果。
前置条件
- 已安装Python
- Scavio API密钥(free tier包含250积分/月,无需信用卡)
步骤1:安装依赖
安装requests以发送HTTP请求:
pip install requests步骤2:发起首次Google搜索
向Scavio Google API端点发送带有查询的POST请求。API返回包含organic results, knowledge graph, People Also Ask等的结构化JSON。
import requests
API_KEY = "your_scavio_api_key"
response = requests.post(
"https://api.scavio.dev/api/v1/search",
headers={
"x-api-key": API_KEY,
"Content-Type": "application/json",
},
json={"query": query},
)
data = response.json()
for result in data.get("organic_results", [])[:5]:
print(f"{result['position']}. {result['title']}")
print(f" {result['link']}\n")步骤3:示例响应
API返回结构化JSON。以下是Google搜索的示例响应:
{
"search_metadata": {
"status": "success",
"total_results": 1240000000
},
"organic_results": [
{
"position": 1,
"title": "Best Noise-Cancelling Headphones of 2026",
"link": "https://example.com/best-headphones",
"snippet": "We tested 30+ headphones to find the best...",
"displayed_link": "example.com"
}
],
"knowledge_graph": {
"title": "Noise-cancelling headphones",
"description": "Active noise-cancelling headphones use..."
},
"people_also_ask": [
{ "question": "What are the best noise cancelling headphones right now?" },
{ "question": "Is noise cancelling bad for your ears?" }
]
}每个字段都是结构化和类型化的,无需HTML解析、CSS选择器或正则提取。您的Python代码可以直接访问任何字段。
步骤4:完整可运行示例
以下是搜索Google并打印结果的完整可运行Python脚本:
"""
Scrape Google search results using Scavio API.
Returns structured JSON with organic results, knowledge graph, People Also Ask, and more.
"""
import requests
import json
API_KEY = "your_scavio_api_key"
def search_google(query: str) -> dict:
response = requests.post(
"https://api.scavio.dev/api/v1/search",
headers={
"x-api-key": API_KEY,
"Content-Type": "application/json",
},
json={"query": query},
)
response.raise_for_status()
return response.json()
if __name__ == "__main__":
results = search_google("best noise cancelling headphones 2026")
print(json.dumps(results, indent=2))为什么使用Scavio而不是直接抓取Google?
- 无需代理管理。 直接抓取需要轮换代理以避免IP封禁。Scavio在服务端处理所有这些。
- 无需CAPTCHA解决。 Google会积极阻止自动化请求。Scavio每次都返回干净数据。
- 结构化JSON输出。 无需HTML解析或CSS选择器维护。每次请求都获得类型化的一致数据。
- 一个API覆盖多平台。 使用同一个API密钥和相同的认证模式搜索Google、Amazon、YouTube和Walmart。
- 包含免费套餐。 250积分/月,无需信用卡。每次搜索消耗1个积分。