blob: b3a9a6092107a4be6e94054d8a33bca365ec3d13 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`robotparser` --- Parser for robots.txt
3=============================================
4
5.. module:: robotparser
6 :synopsis: Loads a robots.txt file and answers questions about fetchability of other URLs.
Christian Heimes895627f2007-12-08 17:28:33 +00007.. sectionauthor:: Skip Montanaro <skip@pobox.com>
Georg Brandl116aa622007-08-15 14:28:22 +00008
9
10.. index::
11 single: WWW
12 single: World Wide Web
13 single: URL
14 single: robots.txt
15
16This module provides a single class, :class:`RobotFileParser`, which answers
17questions about whether or not a particular user agent can fetch a URL on the
Christian Heimesdd15f6c2008-03-16 00:07:10 +000018Web site that published the :file:`robots.txt` file. For more details on the
19structure of :file:`robots.txt` files, see http://www.robotstxt.org/orig.html.
Georg Brandl116aa622007-08-15 14:28:22 +000020
21
22.. class:: RobotFileParser()
23
24 This class provides a set of methods to read, parse and answer questions about a
25 single :file:`robots.txt` file.
26
27
Benjamin Petersone41251e2008-04-25 01:59:09 +000028 .. method:: set_url(url)
Georg Brandl116aa622007-08-15 14:28:22 +000029
30 Sets the URL referring to a :file:`robots.txt` file.
31
32
Benjamin Petersone41251e2008-04-25 01:59:09 +000033 .. method:: read()
Georg Brandl116aa622007-08-15 14:28:22 +000034
35 Reads the :file:`robots.txt` URL and feeds it to the parser.
36
37
Benjamin Petersone41251e2008-04-25 01:59:09 +000038 .. method:: parse(lines)
Georg Brandl116aa622007-08-15 14:28:22 +000039
40 Parses the lines argument.
41
42
Benjamin Petersone41251e2008-04-25 01:59:09 +000043 .. method:: can_fetch(useragent, url)
Georg Brandl116aa622007-08-15 14:28:22 +000044
45 Returns ``True`` if the *useragent* is allowed to fetch the *url* according to
46 the rules contained in the parsed :file:`robots.txt` file.
47
48
Benjamin Petersone41251e2008-04-25 01:59:09 +000049 .. method:: mtime()
Georg Brandl116aa622007-08-15 14:28:22 +000050
51 Returns the time the ``robots.txt`` file was last fetched. This is useful for
52 long-running web spiders that need to check for new ``robots.txt`` files
53 periodically.
54
55
Benjamin Petersone41251e2008-04-25 01:59:09 +000056 .. method:: modified()
Georg Brandl116aa622007-08-15 14:28:22 +000057
58 Sets the time the ``robots.txt`` file was last fetched to the current time.
59
60The following example demonstrates basic use of the RobotFileParser class. ::
61
62 >>> import robotparser
63 >>> rp = robotparser.RobotFileParser()
64 >>> rp.set_url("http://www.musi-cal.com/robots.txt")
65 >>> rp.read()
66 >>> rp.can_fetch("*", "http://www.musi-cal.com/cgi-bin/search?city=San+Francisco")
67 False
68 >>> rp.can_fetch("*", "http://www.musi-cal.com/")
69 True
70