自動でGoogle Search Consoleにはてなブログ記事をインデックス登録する方法です。
インデックス登録が行われなければGoogle 検索によってブログが表示されません。
インデックスの登録はサイトマップを送信することでGoogleにより実施される仕様とのことですが、はてなブログではそのクロールが中々されずインデックス登録されないことがあるらしいです。
簡単な対応方法はSearch Console(Google Search Console)
でブログ記事のURLを直接入力し、インデックス登録をリクエストすることです。
ただ、記事を書くたびにそんな作業を毎回やりたくないのでツールを作って自動化します。
■ツール概要
1.まず始めに記事のURLを取得します。
先日作成した記事の一覧を抽出するツールを使用します。
ikodatech.com
2.Indexing APIを使用して1.で取得したURLに対してインデックス登録リクエストします。
今回ツールはpython(anacondaを使用)で実装します。
■ツール実装
0.下記よりIndexing APIを使用するための準備を行います。
https://developers.google.com/search/apis/indexing-api/v3/prereqs?hl=ja
1.先日作成した記事の一覧を抽出するツール(ps1ファイル)を用意します。
2.同フォルダに下記のpythonファイルを下記で作成します。
尚、必要なモジュールは下記辺りでダウンロード可能です。
https://anaconda.org/conda-forge/google-auth
https://anaconda.org/conda-forge/google-api-python-client
https://anaconda.org/conda-forge/httplib2
from google.oauth2 import service_account from googleapiclient.http import BatchHttpRequest from googleapiclient.discovery import build import httplib2 import os # 0.で取得できるjsonファイルです。 JSON_KEY_FILE = "XXXX.json" SCOPES = [ "https://www.googleapis.com/auth/indexing" ] credentials = service_account.Credentials.from_service_account_file( JSON_KEY_FILE, scopes=SCOPES) service = build('indexing', 'v3', credentials=credentials) def insert_event(request_id, response, exception): print(response ) print(request_id) if exception is not None: # Do something with the exception pass else: # Do something with the response pass # "output_articles.ps1"は1.で用意したps1ファイルです。 os.system('powershell -Command' + ' ' +\ 'powershell -ExecutionPolicy RemoteSigned .\\output_articles.ps1 > articles.txt') articles_list = [] with open('./articles.txt') as f: for line in f: articles_list.append(line.replace("\n","")) batch = service.new_batch_http_request(callback=insert_event) for item in articles_list: batch.add(service.urlNotifications().publish( body={"url": item , "type": "URL_UPDATED"})) batch.execute()