3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SearchConsoleのサイトエラーを巡回してステータスを確認する

Last updated at Posted at 2018-08-05

日々サイトの運用やってて気になるのがSearchConsoleのサイトエラーです。
1件2件なら目視で確認できるけど、何故か大量に出ることがあるのでチェックができなくて困ってしまいます。それならPython使って巡回してステータスだけでも確認したほうが良さげです。
SearchConsoleはGoogleのクローラーが読みに来たタイミングでステータスを計測しているので、今チェックしたらエラーじゃないパターンは結構あります。なので絶対鵜呑みにしてはいけません。

SearchConsoleのクロールエラーの欄には直近発生したエラーが表示されており、1000件までDLできるようになっています。以下のような感じです。

URL,レスポンス コード,ニュースのエラー,最終検出,カテゴリ,プラットフォーム,前回のクロール
https://vegewel.com/ja/area/153,404,,18/06/03,見つかりませんでした,PC,18/08/11
https://vegewel.com/ja/area/162,404,,18/06/22,見つかりませんでした,PC,18/08/05
https://vegewel.com/ja/area/122,404,,18/06/03,見つかりませんでした,PC,18/08/05
https://vegewel.com/ja/area/37,404,,18/06/14,見つかりませんでした,PC,18/07/23
https://vegewel.com/ja/area/69,404,,18/06/02,見つかりませんでした,PC,18/07/28
https://vegewel.com/ja/restaurant/73,404,,16/12/14,見つかりませんでした,PC,18/08/16
https://vegewel.com/en/area/162,404,,18/06/03,見つかりませんでした,PC,18/07/31
https://vegewel.com/ja/restaurant/267,404,,17/02/22,見つかりませんでした,PC,18/08/16
https://vegewel.com/ja/restaurant/635,404,,17/02/25,見つかりませんでした,PC,18/07/04
https://vegewel.com/en/restaurant/129,404,,18/04/26,見つかりませんでした,PC,18/08/05
https://vegewel.com/ja/restaurant/931,404,,18/05/15,見つかりませんでした,PC,18/08/11
https://vegewel.com/ja/restaurant/34,404,,17/02/23,見つかりませんでした,PC,18/08/03

これを全部1ページずつ開いて確認するのは厳しいという話です。

ということで自分のPCがMacという前提で話を進めます。まずはPython3のインストールから。
MacのパッケージインストールではおなじみのBrewを使います。

$ mkdir crawl_checker #巡回プログラム専用のディレクトリ作成
$ cd crawl_checker/
$ which python3 # Pythonの最新版の存在確認
$ brew install python3 # なかったのでインストール
Error: python 2.7.14_3 is already installed
To upgrade to 3.7.0, run `brew upgrade python`
$ brew upgrade python # インストール途中で怒られたのでupgradeで再インストール
$ which python3 # インストールを確認
/usr/local/bin/python3
$ python3 --version # Pythonの詳細バージョンの確認
Python 3.7.0
$ pip3 install requests

ここまで出来たら次はURLを簡単に扱えるRequestsモジュールをインストールします。

$ pip3 install requests

そこまで出来たら以下は巡回用プログラムです。ちなみに現在のcrawl_checkerディレクトリにSearchConsoleのサイトエラーの一覧をCSVダウンロードしたもの(vegewel-com_20180805T003433Z_CrawlErrors.csv)を置いています。

import requests
import csv

csv_file = open("./vegewel-com_20180805T003433Z_CrawlErrors.csv", "r", encoding="utf-8", errors="", newline="" )

f = csv.reader(csv_file, delimiter=",", doublequote=True, lineterminator="\r\n")
header = next(f)
print(header)
for row in f:
    resp = requests.get(row[0])
    print ('Code = {0} URL = {1}'.format(resp.status_code, row[0]))

そして実行。

$ python3 access_test.py
Code = 200 URL = https://vegewel.com/en/knowledge_sprouts/31/
Code = 404 URL = https://vegewel.com/en/station/5248/
Code = 404 URL = https://vegewel.com/en/menu/826
Code = 404 URL = https://vegewel.com/ja/product/17

バッチで定期的に回すもよし。いろいろ応用範囲も多いかと思います。是非ご活用ください。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?