跳转到主要内容

简介

Google Search 是 Ace Data Cloud 提供的搜索服务,Google 搜索 API。通过 Ace Data Cloud 统一 API,你可以使用 Python 快速集成 Google Search,实现网页搜索、图片搜索、新闻搜索、地图搜索、视频搜索等功能。

前置条件

  • 拥有 Ace Data Cloud 账号并获取 API Token
  • Python 3.7 及以上环境
  • 安装 requests 库:pip install requests

基础用法

调用 Google Search API 的主端点为:
POST https://api.acedata.cloud/serp/google
完整 Python 代码示例:
import requests

url = "https://api.acedata.cloud/serp/google"
headers = {
    "Authorization": "Bearer YOUR_API_TOKEN",
    "Content-Type": "application/json"
}
data = {
    "query": "artificial intelligence latest news",
    "type": "search",
    "number": 10,
    "language": "en",
    "country": "US"
}

response = requests.post(url, headers=headers, json=data)
result = response.json()
print(result)
请将 YOUR_API_TOKEN 替换为你在 Ace Data Cloud 平台获取的实际 Token。

响应处理

成功调用后,API 返回 JSON 格式数据。建议检查 HTTP 状态码:
if response.status_code == 200:
    result = response.json()
    print("调用成功:", result)
else:
    print(f"调用失败,状态码: {response.status_code}")
    print(response.text)

进阶用法

对于支持异步任务的 API,可以通过回调 URL 获取结果:
data['callback_url'] = 'https://your-server.com/callback'
response = requests.post(url, headers=headers, json=data)
# 结果将通过回调 URL 推送到你的服务器

错误处理

常见错误码:
状态码说明
401认证失败,请检查 API Token
403余额不足或无权访问
429请求频率超限
500服务器内部错误

下一步