21.10. urllib.robotparser — robots.txt のためのパーザ¶
ソースコード: Lib/urllib/robotparser.py
このモジュールでは単一のクラス、 RobotFileParser を提供します。このクラスは、特定のユーザエージェントが robots.txt ファイルを公開している Web サイトのある URL を取得可能かどうかの質問に答えます。 robots.txt ファイルの構造に関する詳細は http://www.robotstxt.org/orig.html を参照してください。
-
class
urllib.robotparser.RobotFileParser(url='')¶ url の
robots.txtに対し読み込み、パーズ、応答するメソッドを提供します。-
set_url(url)¶ robots.txtファイルを参照するための URL を設定します。
-
read()¶ robots.txtURL を読み出し、パーザに入力します。
-
parse(lines)¶ 引数 lines の内容を解釈します。
-
can_fetch(useragent, url)¶ 解釈された
robots.txtファイル中に記載された規則に従ったとき、 useragent が url を取得してもよい場合にはTrueを返します。
-
mtime()¶ robots.txtファイルを最後に取得した時刻を返します。この値は、定期的に新たなrobots.txtをチェックする必要がある、長時間動作する Web スパイダープログラムを実装する際に便利です。
-
modified()¶ robots.txtファイルを最後に取得した時刻を現在の時刻に設定します。
-
crawl_delay(useragent)¶ Returns the value of the
Crawl-delayparameter fromrobots.txtfor the useragent in question. If there is no such parameter or it doesn’t apply to the useragent specified or therobots.txtentry for this parameter has invalid syntax, returnNone.バージョン 3.6 で追加.
-
request_rate(useragent)¶ Returns the contents of the
Request-rateparameter fromrobots.txtas a named tupleRequestRate(requests, seconds). If there is no such parameter or it doesn’t apply to the useragent specified or therobots.txtentry for this parameter has invalid syntax, returnNone.バージョン 3.6 で追加.
-
The following example demonstrates basic use of the RobotFileParser
class:
>>> import urllib.robotparser
>>> rp = urllib.robotparser.RobotFileParser()
>>> rp.set_url("http://www.musi-cal.com/robots.txt")
>>> rp.read()
>>> rrate = rp.request_rate("*")
>>> rrate.requests
3
>>> rrate.seconds
20
>>> rp.crawl_delay("*")
6
>>> rp.can_fetch("*", "http://www.musi-cal.com/cgi-bin/search?city=San+Francisco")
False
>>> rp.can_fetch("*", "http://www.musi-cal.com/")
True
