blob: fb261231c50320107995f922fb366a01a227c926 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`robotparser` --- Parser for robots.txt
3=============================================
4
5.. module:: robotparser
Skip Montanarodfd98272008-04-28 03:25:37 +00006 :synopsis: Loads a robots.txt file and answers questions about
Georg Brandle8559912008-04-28 05:16:30 +00007 fetchability of other URLs.
Skip Montanaro54662462007-12-08 15:26:16 +00008.. sectionauthor:: Skip Montanaro <skip@pobox.com>
Georg Brandl8ec7f652007-08-15 14:28:01 +00009
10
11.. index::
12 single: WWW
13 single: World Wide Web
14 single: URL
15 single: robots.txt
Brett Cannon963ffdb2008-07-11 00:48:57 +000016
17.. note::
18 The :mod:`robotparser` module has been renamed :mod:`urllib.robotparser` in
19 Python 3.0.
20 The :term:`2to3` tool will automatically adapt imports when converting
21 your sources to 3.0.
Georg Brandl8ec7f652007-08-15 14:28:01 +000022
23This module provides a single class, :class:`RobotFileParser`, which answers
24questions about whether or not a particular user agent can fetch a URL on the
Georg Brandl02677812008-03-15 00:20:19 +000025Web site that published the :file:`robots.txt` file. For more details on the
26structure of :file:`robots.txt` files, see http://www.robotstxt.org/orig.html.
Georg Brandl8ec7f652007-08-15 14:28:01 +000027
28
29.. class:: RobotFileParser()
30
Skip Montanarodfd98272008-04-28 03:25:37 +000031 This class provides a set of methods to read, parse and answer questions
32 about a single :file:`robots.txt` file.
Georg Brandl8ec7f652007-08-15 14:28:01 +000033
34
Benjamin Petersonc7b05922008-04-25 01:29:10 +000035 .. method:: set_url(url)
Georg Brandl8ec7f652007-08-15 14:28:01 +000036
37 Sets the URL referring to a :file:`robots.txt` file.
38
39
Benjamin Petersonc7b05922008-04-25 01:29:10 +000040 .. method:: read()
Georg Brandl8ec7f652007-08-15 14:28:01 +000041
42 Reads the :file:`robots.txt` URL and feeds it to the parser.
43
44
Benjamin Petersonc7b05922008-04-25 01:29:10 +000045 .. method:: parse(lines)
Georg Brandl8ec7f652007-08-15 14:28:01 +000046
47 Parses the lines argument.
48
49
Benjamin Petersonc7b05922008-04-25 01:29:10 +000050 .. method:: can_fetch(useragent, url)
Georg Brandl8ec7f652007-08-15 14:28:01 +000051
Skip Montanarodfd98272008-04-28 03:25:37 +000052 Returns ``True`` if the *useragent* is allowed to fetch the *url*
53 according to the rules contained in the parsed :file:`robots.txt`
54 file.
Georg Brandl8ec7f652007-08-15 14:28:01 +000055
56
Benjamin Petersonc7b05922008-04-25 01:29:10 +000057 .. method:: mtime()
Georg Brandl8ec7f652007-08-15 14:28:01 +000058
Skip Montanarodfd98272008-04-28 03:25:37 +000059 Returns the time the ``robots.txt`` file was last fetched. This is
60 useful for long-running web spiders that need to check for new
61 ``robots.txt`` files periodically.
Georg Brandl8ec7f652007-08-15 14:28:01 +000062
63
Benjamin Petersonc7b05922008-04-25 01:29:10 +000064 .. method:: modified()
Georg Brandl8ec7f652007-08-15 14:28:01 +000065
Skip Montanarodfd98272008-04-28 03:25:37 +000066 Sets the time the ``robots.txt`` file was last fetched to the current
67 time.
Georg Brandl8ec7f652007-08-15 14:28:01 +000068
69The following example demonstrates basic use of the RobotFileParser class. ::
70
71 >>> import robotparser
72 >>> rp = robotparser.RobotFileParser()
73 >>> rp.set_url("http://www.musi-cal.com/robots.txt")
74 >>> rp.read()
75 >>> rp.can_fetch("*", "http://www.musi-cal.com/cgi-bin/search?city=San+Francisco")
76 False
77 >>> rp.can_fetch("*", "http://www.musi-cal.com/")
78 True
79