blob: ba701c3b6897a2ab995133beecc9e36e7daf97a4 [file] [log] [blame]
Senthil Kumaranaca8fd72008-06-23 04:41:59 +00001:mod:`urllib.robotparser` --- Parser for robots.txt
2====================================================
3
4.. module:: urllib.robotparser
Georg Brandl0f7ede42008-06-23 11:23:31 +00005 :synopsis: Load a robots.txt file and answer questions about
Senthil Kumaranaca8fd72008-06-23 04:41:59 +00006 fetchability of other URLs.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007
Senthil Kumaranaca8fd72008-06-23 04:41:59 +00008.. sectionauthor:: Skip Montanaro <skip@pobox.com>
9
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040010**Source code:** :source:`Lib/urllib/robotparser.py`
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000011
12.. index::
13 single: WWW
14 single: World Wide Web
15 single: URL
16 single: robots.txt
17
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040018--------------
19
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000020This module provides a single class, :class:`RobotFileParser`, which answers
21questions about whether or not a particular user agent can fetch a URL on the
22Web site that published the :file:`robots.txt` file. For more details on the
23structure of :file:`robots.txt` files, see http://www.robotstxt.org/orig.html.
24
25
Terry Jan Reedyf3f06812013-03-15 16:50:23 -040026.. class:: RobotFileParser(url='')
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000027
Terry Jan Reedyf3f06812013-03-15 16:50:23 -040028 This class provides methods to read, parse and answer questions about the
29 :file:`robots.txt` file at *url*.
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000030
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000031 .. method:: set_url(url)
32
33 Sets the URL referring to a :file:`robots.txt` file.
34
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000035 .. method:: read()
36
37 Reads the :file:`robots.txt` URL and feeds it to the parser.
38
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000039 .. method:: parse(lines)
40
41 Parses the lines argument.
42
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000043 .. method:: can_fetch(useragent, url)
44
45 Returns ``True`` if the *useragent* is allowed to fetch the *url*
46 according to the rules contained in the parsed :file:`robots.txt`
47 file.
48
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000049 .. method:: mtime()
50
51 Returns the time the ``robots.txt`` file was last fetched. This is
52 useful for long-running web spiders that need to check for new
53 ``robots.txt`` files periodically.
54
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000055 .. method:: modified()
56
57 Sets the time the ``robots.txt`` file was last fetched to the current
58 time.
59
Georg Brandl0f7ede42008-06-23 11:23:31 +000060
61The following example demonstrates basic use of the RobotFileParser class.
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000062
63 >>> import urllib.robotparser
64 >>> rp = urllib.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