こんにちは、ミナピピン(@python_mllover)です。
今回はGoogle検索をPythonを使用してAPI経由で行う方法について紹介したいと思います。
事前準備
API鍵の作成
参考記事:【GCP】GoogleCloudPlatformでプロジェクトを作成してAPI鍵の作成&有効化する
カスタム検索エンジン(CSE)の作成と検索エンジンIDの取得
CUSTOM SEARCH APIを使用するためには他のAPIと違ってカスタム検索エンジン(CSE)の作成と検索エンジンIDの取得という手順が必要になります。
⇒https://plus-pm.net/search-tracking-google-search-api/#toc21
import os from datetime import datetime import json from googleapiclient.discovery import build import pprint GOOGLE_API_KEY = '' CUSTOM_SEARCH_ENGINE_ID = '' KEYWORD = 'ガンダムSEED' # Google Customサーチ結果を取得 s = build('customsearch', 'v1', developerKey = GOOGLE_API_KEY) r = s.cse().list(q = KEYWORD, cx = CUSTOM_SEARCH_ENGINE_ID, lr = 'lang_ja', num = 10, start = 1).execute() pprint.pprint(r['items'])
APIドキュメント:https://developers.google.com/custom-search/v1/introduction
これでGoogleの検索結果が辞書型形式で取得できています。
参考:https://qiita.com/zak_y/items/42ca0f1ea14f7046108c
コメント