Webクローリングを行う際にはrobots.txtを読み込まなければなりません。それによって許可されているコンテンツだけを対象とするのが行儀の良いクローラーです。Googleのクローラーは、数多あるrobots.txtを読み込んできた先駆者とも言えるでしょう。 今回紹介するGoogle Robots.txt Parser and Matcher LibraryははそんなGoogleのクローラーで使っているrobots.txtのパーサーライブラリをオープンソース化したものです。

Google Robots.txt Parser and Matcher Libraryの使い方

実行方法はrobots.txtのパスとクローラー名、そして対象になるURLを指定します。

$ bazel run robots_main -- /path/to/robots.txt GoogleBot https://www.amazon.co.jp/
INFO: Analyzed target //:robots_main (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //:robots_main up-to-date:
  bazel-bin/robots_main
INFO: Elapsed time: 0.111s, Critical Path: 0.00s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
user-agent 'GoogleBot' with URI 'https://www.amazon.co.jp/': ALLOWED

例えばアクセス禁止に設定されているパスの場合はエラーになります。

$ bazel run robots_main -- /path/to/robots.txt GoogleBot https://www.amazon.co.jp/o/dt
  :
user-agent 'GoogleBot' with URI 'https://www.amazon.co.jp/o/dt': DISALLOWED

なぜかAmazonから完全に締め出されているEtaoSpiderの場合、ユーザエージェントレベルで弾かれます。

$ bazel run robots_main -- /path/to/robots.txt EtaoSpider https://www.amazon.co.jp/
  :
user-agent 'EtaoSpider' with URI 'https://www.amazon.co.jp/': DISALLOWED

Google Robots.txt Parser and Matcher Libraryは様々な人たちが記述したrobots.txtを読み込んできたパーサーです。そしてアクセスの有効、無効についても判定してきたアルゴリズムがあります。クローラーを作成する際には参考になりそうです。

Google Robots.txt Parser and Matcher LibraryはC++製のオープンソース・ソフトウェア(Apache Licnese 2.0)です。

google/robotstxt: The repository contains Google’s robots.txt parser and matcher as a C++ library (compliant to C++11).