Google検索をCLIで実現するCustom Search JSON APIを試してみた

Google検索をCLIで実現するCustom Search JSON APIを試してみた

Google検索結果の自動取得や、特定のURLのインデックス登録状況確認に、Custom Search JSON APIをCLIから利用してみました。
Clock Icon2025.03.08

2025年1月、Googleは検索エンジンを使用するユーザーに対してJavaScriptの有効化を求める変更があり、
従来のcurlコマンドを用いたGoogle検索結果の取得が困難になりました。

https://gigazine.net/news/20250121-google-requiring-javascript/

https://techcrunch.com/2025/01/17/google-begins-requiring-javascript-for-google-search/

この結果、従来 curlコマンドを利用して実施していたGoogle検索では、JavaScript要素のみが戻るようになり、検索結果を確認する事ができない状態になりました。

Google検索Curl実行イメージ

今回、代替策として Google Custom Search JSON APIを用いたGoogle検索結果の取得を試す機会がありましたので、紹介します。

Google Cloud Platform

Google Cloud Platform(GCP)プロジェクトで以下の設定を行いました。

  • Custom Search APIの 有効化
  • Custom Search API用のAPIキーの作成

API有効化

ナビゲーションメニューから「API とサービス」→「ライブラリ」を選択。

「Custom Search API」を検索

Custom Search API 検索

「Custom Search API」、1日100回以上のクエリに対する課金体系などを確認し、有効にしました。

Custom Search API 有効化

APIキーの作成

「API とサービス」→「認証情報」 の画面で、「認証情報を作成」→「API キー」を作成しました。

APIキー作成

作成したAPIキー、任意の名前を付与。
Custom Search API の利用のみに制限する設定を行いました。

APIキー編集

Programmable Search Engine

Custom Search APIの利用に必要な検索エンジンIDを取得するため、任意のGoogleアカウントにログインした状態でProgrammable Search Engineにアクセス。
検索エンジンを作成してIDを取得しました。

検索エンジンの追加

  • 最初の検索エンジンを作成しました。

Programmable Search Engine追加

  • 任意のエンジン名と、特定ドメインのサイト内検索でするため、サイト指定を行いました。

Programmable Search Engine作成

  • 検索エンジン作成後、「カスタマイズ」の画面に遷移

Programmable Search Engine作成完了

  • 検索エンジンIDを確認し控えました。

Programmable Search Engine概要

動作確認

Custom Search API のエンドポイントに対し、以下のパラメータを付与してサイト内検索が可能な事を確認しました。

エンドポイント

https://customsearch.googleapis.com/customsearch/v1

クエストパラメータ

  • key: 作成した API キー
  • cx: 検索エンジン ID
  • q: 検索クエリ

実行例

API_KEY='********'
CSE_ID='********'
ENDPOINT="https://customsearch.googleapis.com/customsearch/v1"
QUERY="anthropic"
curl -s "${ENDPOINT}?key=${API_KEY}&cx=${CSE_ID}&q=${QUERY}" > output.json
~ $ cat output.json | jq '.items[] | {title: .title, link: .link}'
{
  "title": "Unlocking Human-like Understanding: A Guide to Anthropic Claude ...",
  "link": "https://dev.classmethod.jp/articles/unlocking-human-like-understanding-a-guide-to-anthropic-claude/"
}
{
  "title": "Anthropic Introduces New Business-Focused Solutions and Debuts ...",
  "link": "https://dev.classmethod.jp/articles/anthropic-introduces-new-business-focused-solutions-and-debuts-iphone-application/"
}
{
  "title": "Getting Started with Dora AI! | DevelopersIO",
  "link": "https://dev.classmethod.jp/articles/getting-started-with-dora-ai/"
}
{
  "title": "[アップデート]Amazon BedrockでAnthropicの最新モデルClaude 2.1 ...",
  "link": "https://dev.classmethod.jp/articles/update-bedrock-anthropic-claude2-1-ga/"
}
{
  "title": "Anthropic アップデートのAWS対応状況まとめ | DevelopersIO",
  "link": "https://dev.classmethod.jp/articles/anthropic-update-aws/"
}
{
  "title": "Claude 3.5 Sonnet、Anthropic社の最新生成AIモデルを試してみた ...",
  "link": "https://dev.classmethod.jp/articles/claude-35-sonnet-new-anthropic-ai-model/"
}
{
  "title": "Anthropic の記事一覧 | DevelopersIO",
  "link": "https://dev.classmethod.jp/tags/anthropic/"
}
{
  "title": "Anthropic Claude 3 Sonnet 使ってみた | DevelopersIO",
  "link": "https://dev.classmethod.jp/articles/anthropic-claude-3-sonnet-used-by/"
}
{
  "title": "Anthropic Claude 3 の画像処理をBedrockで試してみた | DevelopersIO",
  "link": "https://dev.classmethod.jp/articles/claude-3-image-processing/"
}
{
  "title": "Amazon Bedrockで使用できるClaudeモデルのトークン数を ...",
  "link": "https://dev.classmethod.jp/articles/claude-token-count/"
}

引数として記事URLを指定して、登録されているインデックス情報を確認する事もできました。

QUERY="https://dev.classmethod.jp/articles/llms-txt-for-ai-crawlers/"
curl -s "${ENDPOINT}?key=${API_KEY}&cx=${CSE_ID}&q=${QUERY}" > output.json
cat output.json | jq ".items[] | select(.link == \"${QUERY}\")"

URL検索

まとめ

Google Custom Search JSON APIを利用することで、Google検索や登録済みのインデックスの確認、Google Search ConsoleのURL検査相当の機能をCLIから実現できました。

注意点として、無料枠は1日100クエリ、有料でも1日10,000クエリまでの回数制限があります。

また今回はAPIキーを利用しましたが、セキュリティリスクを減らすための適切な制限設定や、管理にも十分注意する事をおすすめします。

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.