<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>HTTP Redirects — feedparser 5.1.3 documentation</title> <link rel="stylesheet" href="_static/default.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <link rel="stylesheet" href="_static/feedparser.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '', VERSION: '5.1.3', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="_static/jquery.js"></script> <script type="text/javascript" src="_static/underscore.js"></script> <script type="text/javascript" src="_static/doctools.js"></script> <link rel="top" title="feedparser 5.1.3 documentation" href="index.html" /> <link rel="up" title="HTTP Features" href="http.html" /> <link rel="next" title="Password-Protected Feeds" href="http-authentication.html" /> <link rel="prev" title="User-Agent and Referer Headers" href="http-useragent.html" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="http-authentication.html" title="Password-Protected Feeds" accesskey="N">next</a> |</li> <li class="right" > <a href="http-useragent.html" title="User-Agent and Referer Headers" accesskey="P">previous</a> |</li> <li><a href="index.html">feedparser 5.1.3 documentation</a> »</li> <li><a href="http.html" accesskey="U"><abbr title="Hypertext Transfer Protocol">HTTP</abbr> Features</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="http-redirects"> <h1><abbr title="Hypertext Transfer Protocol">HTTP</abbr> Redirects<a class="headerlink" href="#http-redirects" title="Permalink to this headline">¶</a></h1> <p>When you download a feed from a remote web server, <strong class="program">Universal Feed Parser</strong> exposes the <abbr title="Hypertext Transfer Protocol">HTTP</abbr> status code. You need to understand the different codes, including permanent and temporary redirects, and feeds that have been marked “gone”.</p> <p>When a feed has temporarily moved to a new location, the web server will return a <tt class="docutils literal"><span class="pre">302</span></tt> status code. <strong class="program">Universal Feed Parser</strong> makes this available in <tt class="docutils literal"><span class="pre">d.status</span></tt>.</p> <p>There is nothing special you need to do with temporary redirects; by the time you learn about it, <strong class="program">Universal Feed Parser</strong> has already followed the redirect to the new location (available in <tt class="docutils literal"><span class="pre">d.href</span></tt>), downloaded the feed, and parsed it. Since the redirect is temporary, you should continue requesting the original <abbr title="Uniform Resource Locator">URL</abbr> the next time you want to parse the feed.</p> <div class="section" id="noticing-temporary-redirects"> <h2>Noticing temporary redirects<a class="headerlink" href="#noticing-temporary-redirects" title="Permalink to this headline">¶</a></h2> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">feedparser</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">feedparser</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s">'http://feedparser.org/docs/examples/temporary.xml'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">status</span> <span class="go">302</span> <span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">href</span> <span class="go">'http://feedparser.org/docs/examples/atom10.xml'</span> <span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">feed</span><span class="o">.</span><span class="n">title</span> <span class="go">u'Sample Feed'</span> </pre></div> </div> <p>When a feed has permanently moved to a new location, the web server will return a <tt class="docutils literal"><span class="pre">301</span></tt> status code. Again, <strong class="program">Universal Feed Parser</strong> makes this available in <tt class="docutils literal"><span class="pre">d.status</span></tt>.</p> <p>If you are polling a feed on a regular basis, it is very important to check the status code (<tt class="docutils literal"><span class="pre">d.status</span></tt>) every time you download. If the feed has been permanently redirected, you should update your database or configuration file with the new address (<tt class="docutils literal"><span class="pre">d.href</span></tt>). Repeatedly requesting the original address of a feed that has been permanently redirected is very rude, and may get you banned from the server.</p> </div> <div class="section" id="noticing-permanent-redirects"> <h2>Noticing permanent redirects<a class="headerlink" href="#noticing-permanent-redirects" title="Permalink to this headline">¶</a></h2> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">feedparser</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">feedparser</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s">'http://feedparser.org/docs/examples/permanent.xml'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">status</span> <span class="go">301</span> <span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">href</span> <span class="go">'http://feedparser.org/docs/examples/atom10.xml'</span> <span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">feed</span><span class="o">.</span><span class="n">title</span> <span class="go">u'Sample Feed'</span> </pre></div> </div> <p>When a feed has been permanently deleted, the web server will return a <tt class="docutils literal"><span class="pre">410</span></tt> status code. If you ever receive a <tt class="docutils literal"><span class="pre">410</span></tt>, you should stop polling the feed and inform the end user that the feed is gone for good.</p> <p>Repeatedly requesting a feed that has been marked as “gone” is very rude, and may get you banned from the server.</p> </div> <div class="section" id="noticing-feeds-marked-gone"> <h2>Noticing feeds marked “gone”<a class="headerlink" href="#noticing-feeds-marked-gone" title="Permalink to this headline">¶</a></h2> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">feedparser</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">feedparser</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s">'http://feedparser.org/docs/examples/gone.xml'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">status</span> <span class="go">410</span> </pre></div> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="index.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#"><abbr title="Hypertext Transfer Protocol">HTTP</abbr> Redirects</a><ul> <li><a class="reference internal" href="#noticing-temporary-redirects">Noticing temporary redirects</a></li> <li><a class="reference internal" href="#noticing-permanent-redirects">Noticing permanent redirects</a></li> <li><a class="reference internal" href="#noticing-feeds-marked-gone">Noticing feeds marked “gone”</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="http-useragent.html" title="previous chapter">User-Agent and Referer Headers</a></p> <h4>Next topic</h4> <p class="topless"><a href="http-authentication.html" title="next chapter">Password-Protected Feeds</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="_sources/http-redirect.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="http-authentication.html" title="Password-Protected Feeds" >next</a> |</li> <li class="right" > <a href="http-useragent.html" title="User-Agent and Referer Headers" >previous</a> |</li> <li><a href="index.html">feedparser 5.1.3 documentation</a> »</li> <li><a href="http.html" ><abbr title="Hypertext Transfer Protocol">HTTP</abbr> Features</a> »</li> </ul> </div> <div class="footer"> © Copyright 2004-2008 Mark Pilgrim, 2010-2012 Kurt McKee. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>