メインコンテンツへスキップ

はじめに

Google Search は Ace Data Cloud が提供する検索サービスであり、Google 検索 API です。Ace Data Cloud の統一 API を通じて、Python を使って Google Search を素早く統合し、ウェブ検索、画像検索、ニュース検索、地図検索、動画検索などの機能を実現できます。

前提条件

  • Ace Data Cloud アカウントを持ち、API トークンを取得していること
  • 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 プラットフォームで取得した実際のトークンに置き換えてください。

レスポンス処理

呼び出しが成功すると、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 トークンを確認してください
403残高不足またはアクセス権限なし
429リクエスト頻度超過
500サーバー内部エラー

次のステップ