Created
November 9, 2021 23:31
-
-
Save jshirius/8eea51730fdf58e950e0857e760dd290 to your computer and use it in GitHub Desktop.
google search consoleのサンプルコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#リファレンスの場所 | |
#https://developers.google.com/webmaster-tools/search-console-api-original/v3/quickstart/quickstart-python | |
import pandas as pd | |
from googleapiclient.discovery import build | |
from oauth2client.service_account import ServiceAccountCredentials | |
url = ['https://www.googleapis.com/auth/webmasters.readonly'] | |
file = 'ダウンロードしたjsonファイルの場所' | |
credentials = ServiceAccountCredentials.from_json_keyfile_name( file, url) | |
webmasters = build('webmasters', 'v3', credentials=credentials) | |
#Google search Consoleで設定したドメイン(url)を設定 | |
#間違えると以下のようなエラーが吐き出させる(筆者は、http,httpsで間違えた) | |
""" | |
https、httpの間違いで出力された | |
HttpError: <HttpError 403 when requesting https://www.googleapis.com/webmasters/v3/sites/http〜/searchAnalytics/query?alt=json returned "User does not have sufficient permission for site 'http〜", 'domain': 'global', 'reason': 'forbidden'}]"> | |
""" | |
domain = 'Google search Consoleで設定したドメイン(url)を設定 http,httpsの記載ミスに注意 記入例:https://sample.jp' | |
#今回は検索キーワードと該当するページを出力する | |
#他にも「date」を設定することで日付ごとに出力することも可能。 | |
#詳しくは以下の公式マニュアルを参考に | |
#https://developers.google.com/webmaster-tools/search-console-api-original/v3/how-tos/search_analytics | |
dimensions = ['query', 'page'] | |
startDate = '2021-11-01' | |
endDate = '2021-11-10' | |
rowLimit = 1000 | |
body = { | |
'startDate': startDate, | |
'endDate': endDate, | |
'dimensions': dimensions, | |
'rowLimit': rowLimit | |
} | |
#クエリする | |
response = webmasters.searchanalytics().query(siteUrl=domain, body=body).execute() | |
#json_normalizeを使うと、ネストした(入れ子になった)dict型のデータをキーごとに個別のカラムとして認識してくれる | |
#参考サイト | |
#https://note.nkmk.me/python-pandas-json-normalize/ | |
df = pd.io.json.json_normalize(response['rows']) | |
#カラムkeysは、 queryとpageが一緒になっているので、個別のカラムにする | |
for i, dimension in enumerate(dimensions): | |
df[dimension] = df['keys'].apply(lambda x: x[i]) | |
#keysは用無しなので消す | |
df.drop(columns='keys', inplace=True) | |
df.to_csv("result_search-console.csv", index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment