blob: 1a66955f0be0ddfc9fed18eb6941cd309dcb93a8 [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.
7.. sectionauthor:: Skip Montanaro <skip@mojam.com>
8
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
18Web site that published the :file:`robots.txt` file. For more details on the
19structure of :file:`robots.txt` files, see
20http://www.robotstxt.org/wc/norobots.html.
21
22
23.. class:: RobotFileParser()
24
25 This class provides a set of methods to read, parse and answer questions about a
26 single :file:`robots.txt` file.
27
28
29 .. method:: RobotFileParser.set_url(url)
30
31 Sets the URL referring to a :file:`robots.txt` file.
32
33
34 .. method:: RobotFileParser.read()
35
36 Reads the :file:`robots.txt` URL and feeds it to the parser.
37
38
39 .. method:: RobotFileParser.parse(lines)
40
41 Parses the lines argument.
42
43
44 .. method:: RobotFileParser.can_fetch(useragent, url)
45
46 Returns ``True`` if the *useragent* is allowed to fetch the *url* according to
47 the rules contained in the parsed :file:`robots.txt` file.
48
49
50 .. method:: RobotFileParser.mtime()
51
52 Returns the time the ``robots.txt`` file was last fetched. This is useful for
53 long-running web spiders that need to check for new ``robots.txt`` files
54 periodically.
55
56
57 .. method:: RobotFileParser.modified()
58
59 Sets the time the ``robots.txt`` file was last fetched to the current time.
60
61The following example demonstrates basic use of the RobotFileParser class. ::
62
63 >>> import robotparser
64 >>> rp = robotparser.RobotFileParser()
65 >>> rp.set_url("http://www.musi-cal.com/robots.txt")
66 >>> rp.read()
67 >>> rp.can_fetch("*", "http://www.musi-cal.com/cgi-bin/search?city=San+Francisco")
68 False
69 >>> rp.can_fetch("*", "http://www.musi-cal.com/")
70 True
71