1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>13.3. robotparser — Parser for robots.txt — Python 2.7.12 documentation</title>
11
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 2.7.12 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 2.7.12 documentation" href="../contents.html" />
33
<link rel="up" title="13. File Formats" href="fileformats.html" />
34
<link rel="next" title="13.4. netrc — netrc file processing" href="netrc.html" />
35
<link rel="prev" title="13.2. ConfigParser — Configuration file parser" href="configparser.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="netrc.html" title="13.4. netrc — netrc file processing"
55
accesskey="N">next</a> |</li>
57
<a href="configparser.html" title="13.2. ConfigParser — Configuration file parser"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">2.7.12</span>
64
<a href="../index.html">Documentation</a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
68
<li class="nav-item nav-item-2"><a href="fileformats.html" accesskey="U">13. File Formats</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
77
<div class="section" id="module-robotparser">
78
<span id="robotparser-parser-for-robots-txt"></span><h1>13.3. <a class="reference internal" href="#module-robotparser" title="robotparser: Loads a robots.txt file and answers questions about fetchability of other URLs."><code class="xref py py-mod docutils literal"><span class="pre">robotparser</span></code></a> — Parser for robots.txt<a class="headerlink" href="#module-robotparser" title="Permalink to this headline">¶</a></h1>
79
<div class="admonition note" id="index-0">
80
<p class="first admonition-title">Note</p>
81
<p class="last">The <a class="reference internal" href="#module-robotparser" title="robotparser: Loads a robots.txt file and answers questions about fetchability of other URLs."><code class="xref py py-mod docutils literal"><span class="pre">robotparser</span></code></a> module has been renamed <code class="xref py py-mod docutils literal"><span class="pre">urllib.robotparser</span></code> in
83
The <a class="reference internal" href="../glossary.html#term-2to3"><span class="xref std std-term">2to3</span></a> tool will automatically adapt imports when converting
84
your sources to Python 3.</p>
86
<p>This module provides a single class, <a class="reference internal" href="#robotparser.RobotFileParser" title="robotparser.RobotFileParser"><code class="xref py py-class docutils literal"><span class="pre">RobotFileParser</span></code></a>, which answers
87
questions about whether or not a particular user agent can fetch a URL on the
88
Web site that published the <code class="file docutils literal"><span class="pre">robots.txt</span></code> file. For more details on the
89
structure of <code class="file docutils literal"><span class="pre">robots.txt</span></code> files, see <a class="reference external" href="http://www.robotstxt.org/orig.html">http://www.robotstxt.org/orig.html</a>.</p>
91
<dt id="robotparser.RobotFileParser">
92
<em class="property">class </em><code class="descclassname">robotparser.</code><code class="descname">RobotFileParser</code><span class="sig-paren">(</span><em>url=''</em><span class="sig-paren">)</span><a class="headerlink" href="#robotparser.RobotFileParser" title="Permalink to this definition">¶</a></dt>
93
<dd><p>This class provides methods to read, parse and answer questions about the
94
<code class="file docutils literal"><span class="pre">robots.txt</span></code> file at <em>url</em>.</p>
96
<dt id="robotparser.RobotFileParser.set_url">
97
<code class="descname">set_url</code><span class="sig-paren">(</span><em>url</em><span class="sig-paren">)</span><a class="headerlink" href="#robotparser.RobotFileParser.set_url" title="Permalink to this definition">¶</a></dt>
98
<dd><p>Sets the URL referring to a <code class="file docutils literal"><span class="pre">robots.txt</span></code> file.</p>
102
<dt id="robotparser.RobotFileParser.read">
103
<code class="descname">read</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#robotparser.RobotFileParser.read" title="Permalink to this definition">¶</a></dt>
104
<dd><p>Reads the <code class="file docutils literal"><span class="pre">robots.txt</span></code> URL and feeds it to the parser.</p>
108
<dt id="robotparser.RobotFileParser.parse">
109
<code class="descname">parse</code><span class="sig-paren">(</span><em>lines</em><span class="sig-paren">)</span><a class="headerlink" href="#robotparser.RobotFileParser.parse" title="Permalink to this definition">¶</a></dt>
110
<dd><p>Parses the lines argument.</p>
114
<dt id="robotparser.RobotFileParser.can_fetch">
115
<code class="descname">can_fetch</code><span class="sig-paren">(</span><em>useragent</em>, <em>url</em><span class="sig-paren">)</span><a class="headerlink" href="#robotparser.RobotFileParser.can_fetch" title="Permalink to this definition">¶</a></dt>
116
<dd><p>Returns <code class="docutils literal"><span class="pre">True</span></code> if the <em>useragent</em> is allowed to fetch the <em>url</em>
117
according to the rules contained in the parsed <code class="file docutils literal"><span class="pre">robots.txt</span></code>
122
<dt id="robotparser.RobotFileParser.mtime">
123
<code class="descname">mtime</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#robotparser.RobotFileParser.mtime" title="Permalink to this definition">¶</a></dt>
124
<dd><p>Returns the time the <code class="docutils literal"><span class="pre">robots.txt</span></code> file was last fetched. This is
125
useful for long-running web spiders that need to check for new
126
<code class="docutils literal"><span class="pre">robots.txt</span></code> files periodically.</p>
130
<dt id="robotparser.RobotFileParser.modified">
131
<code class="descname">modified</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#robotparser.RobotFileParser.modified" title="Permalink to this definition">¶</a></dt>
132
<dd><p>Sets the time the <code class="docutils literal"><span class="pre">robots.txt</span></code> file was last fetched to the current
138
<p>The following example demonstrates basic use of the RobotFileParser class.</p>
139
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">robotparser</span>
140
<span class="gp">>>> </span><span class="n">rp</span> <span class="o">=</span> <span class="n">robotparser</span><span class="o">.</span><span class="n">RobotFileParser</span><span class="p">()</span>
141
<span class="gp">>>> </span><span class="n">rp</span><span class="o">.</span><span class="n">set_url</span><span class="p">(</span><span class="s2">"http://www.musi-cal.com/robots.txt"</span><span class="p">)</span>
142
<span class="gp">>>> </span><span class="n">rp</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
143
<span class="gp">>>> </span><span class="n">rp</span><span class="o">.</span><span class="n">can_fetch</span><span class="p">(</span><span class="s2">"*"</span><span class="p">,</span> <span class="s2">"http://www.musi-cal.com/cgi-bin/search?city=San+Francisco"</span><span class="p">)</span>
144
<span class="go">False</span>
145
<span class="gp">>>> </span><span class="n">rp</span><span class="o">.</span><span class="n">can_fetch</span><span class="p">(</span><span class="s2">"*"</span><span class="p">,</span> <span class="s2">"http://www.musi-cal.com/"</span><span class="p">)</span>
146
<span class="go">True</span>
155
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
156
<div class="sphinxsidebarwrapper">
157
<h4>Previous topic</h4>
158
<p class="topless"><a href="configparser.html"
159
title="previous chapter">13.2. <code class="docutils literal"><span class="pre">ConfigParser</span></code> — Configuration file parser</a></p>
161
<p class="topless"><a href="netrc.html"
162
title="next chapter">13.4. <code class="docutils literal"><span class="pre">netrc</span></code> — netrc file processing</a></p>
164
<ul class="this-page-menu">
165
<li><a href="../bugs.html">Report a Bug</a></li>
166
<li><a href="../_sources/library/robotparser.txt"
167
rel="nofollow">Show Source</a></li>
170
<div id="searchbox" style="display: none" role="search">
171
<h3>Quick search</h3>
172
<form class="search" action="../search.html" method="get">
173
<input type="text" name="q" />
174
<input type="submit" value="Go" />
175
<input type="hidden" name="check_keywords" value="yes" />
176
<input type="hidden" name="area" value="default" />
178
<p class="searchtip" style="font-size: 90%">
179
Enter search terms or a module, class or function name.
182
<script type="text/javascript">$('#searchbox').show(0);</script>
185
<div class="clearer"></div>
187
<div class="related" role="navigation" aria-label="related navigation">
190
<li class="right" style="margin-right: 10px">
191
<a href="../genindex.html" title="General Index"
194
<a href="../py-modindex.html" title="Python Module Index"
197
<a href="netrc.html" title="13.4. netrc — netrc file processing"
200
<a href="configparser.html" title="13.2. ConfigParser — Configuration file parser"
202
<li><img src="../_static/py.png" alt=""
203
style="vertical-align: middle; margin-top: -1px"/></li>
204
<li><a href="https://www.python.org/">Python</a> »</li>
206
<span class="version_switcher_placeholder">2.7.12</span>
207
<a href="../index.html">Documentation</a> »
210
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
211
<li class="nav-item nav-item-2"><a href="fileformats.html" >13. File Formats</a> »</li>
215
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
217
The Python Software Foundation is a non-profit corporation.
218
<a href="https://www.python.org/psf/donations/">Please donate.</a>
220
Last updated on Sep 20, 2016.
221
<a href="../bugs.html">Found a bug</a>?
223
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'