LinkCheckerを使ってリンク切れチェック(Mac編)

    こんにちは、中村です。

    今回は、リンク切れをチェックするLinkCheckerについて紹介します。

    ■リンク切れとは?

    web界隈で働いている方なら1度ならず何度も聞いたことがあるかと思いますが、
    ページ上にあるリンク先が削除されたなどの理由で、無効になっている症状を言います。
    エラーメッセージとしてはHTTPのステータスコードが404などであれば、NotFoundなどが表示されたりします。

    ■LinkCheckerとは

    リンク切れのチェックサイトやツールはいくつかありますが、一番使い勝手の良かったのがこのツールです。

    ・LinkChecker

    https://wummel.github.io/linkchecker/
    python形式で書かれたオープンソースになります。
    ※GPLライセンスのため使用の際は注意して使ってください

    ・使い方

    以下コマンドで使うことができます。

    ・LinkCheckerのインストール

    pip install linkchecker
    pip install requests==2.9.2

    これだけでインストール完了です。
    簡単!

    ・実行方法

    “linkchecker”コマンドで各種操作が可能です。

    ・linkチェックの実行コマンド

    linkchecker -r1 -t1 -otext http://example.com/test.html >> result.txt

    それぞれの説明として、
    r:チェックする階層レベルです。1を指定するとその階層(1階層目)をチェックします。未指定だと全てをチェックします。
    t:処理するスレッド数を指定します。デフォルトは10。処理が増えすぎないように1を指定しています。
    o:メッセージ表示させるタイプを指定します。text、html、sql、csv、xml、sitemapなど様々な指定が可能です。

    他にもbasic認証やCookieの指定をしたリクエストができたりなどができ、様々な操作が可能です。

    ・成功時

    成功の時は以下のような結果が出ます。
    LinkChecker 9.3 Copyright (C) 2000-2014 Bastian Kleineidam
    LinkChecker comes with ABSOLUTELY NO WARRANTY!
    This is free software, and you are welcome to redistribute it
    under certain conditions. Look at the file `LICENSE' within this
    distribution.
    Get the newest version at http://wummel.github.io/linkchecker/
    Write comments and bugs to https://github.com/wummel/linkchecker/issues
    Support this project at http://wummel.github.io/linkchecker/donations.html
    Start checking at 2018-09-11 19:21:09+009
    Statistics:
    Downloaded: 4KB.
    Content types: 21 image, 6 text, 0 video, 0 audio, 0 application, 0 mail and 1 other.
    URL lengths: min=59, max=101, avg=95.
    That's it. 28 links in 21 URLs checked. 0 warnings found. 0 errors found.
    Stopped checking at 2018-09-11 19:21:15+009 (6 seconds)

    ・エラー発生時

    エラーの時は以下のような結果が出ます。
    LinkChecker 9.3 Copyright (C) 2000-2014 Bastian Kleineidam
    LinkChecker comes with ABSOLUTELY NO WARRANTY!
    This is free software, and you are welcome to redistribute it
    under certain conditions. Look at the file `LICENSE' within this
    distribution.
    Get the newest version at http://wummel.github.io/linkchecker/
    Write comments and bugs to https://github.com/wummel/linkchecker/issues
    Support this project at http://wummel.github.io/linkchecker/donations.html
    Start checking at 2018-09-11 19:24:37+009
    URL `result/test.jpg'
    Parent URL http://example.com/test.html, line 57, col 13
    Real URL http://example.com/test.html/result/test.jpg
    Check time 0.269 seconds
    Size 364B
    Result Error: 404 Not Found
    Statistics:
    Downloaded: 4KB.
    Content types: 23 image, 7 text, 0 video, 0 audio, 0 application, 0 mail and 1 other.
    URL lengths: min=59, max=101, avg=95.
    That's it. 31 links in 19 URLs checked. 0 warnings found. 1 error found.
    Stopped checking at 2018-09-11 19:24:46+009 (8 seconds)

    エラーの時は該当のURL、httpステータスコードなども見れ、詳細な状況が把握できます。

    ・その他LinkCheckerの操作

    ヘルプ情報を見れば他の操作についても確認できます。
    linkchecker -h

    ・注意点

    PCからプログラム上でサーバーに対してアクセスをするため、使用の際は注意が必要です。
    処理スレッドが多すぎたりすると、対象のサーバーに対して攻撃行為として扱われる可能性などもあります。
    あくまでも自身で管理しているシステムに対してのみ、かつ自己責任で行ってください。

    以上です!

    以下参考にさせていただきました。
    https://hostingstock.net/blog/20160616/
    https://orebibou.com/2017/07/webサイトを巡回してリンク切れを確認するpython製のcli/