CINXE.COM
pyquery: a jquery-like library for python — pyquery 1.2.4 documentation
<!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>pyquery: a jquery-like library for python — pyquery 1.2.4 documentation</title> <link rel="stylesheet" href="_static/nature.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '', VERSION: '1.2.4', 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="pyquery 1.2.4 documentation" href="#" /> <link rel="next" title="Attributes" href="attributes.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="py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="attributes.html" title="Attributes" accesskey="N">next</a> |</li> <li><a href="#">pyquery 1.2.4 documentation</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="pyquery-a-jquery-like-library-for-python"> <h1>pyquery: a jquery-like library for python<a class="headerlink" href="#pyquery-a-jquery-like-library-for-python" title="Permalink to this headline">露</a></h1> <p>pyquery allows you to make jquery queries on xml documents. The API is as much as possible the similar to jquery. pyquery uses lxml for fast xml and html manipulation.</p> <p>This is not (or at least not yet) a library to produce or interact with javascript code. I just liked the jquery API and I missed it in python so I told myself “Hey let’s make jquery in python”. This is the result.</p> <p>It can be used for many purposes, one idea that I might try in the future is to use it for templating with pure http templates that you modify using pyquery. I can also be used for web scrapping or for theming applications with <a class="reference external" href="http://www.gawel.org/weblog/en/2008/12/skinning-with-pyquery-and-deliverance">Deliverance</a>.</p> <p>The <a class="reference external" href="https://github.com/gawel/pyquery/">project</a> is being actively developped on a git repository on Github. I have the policy of giving push access to anyone who wants it and then to review what he does. So if you want to contribute just email me.</p> <p>Please report bugs on the <a class="reference external" href="https://github.com/gawel/pyquery/issues">github</a> issue tracker.</p> </div> <div class="section" id="quickstart"> <h1>Quickstart<a class="headerlink" href="#quickstart" title="Permalink to this headline">露</a></h1> <p>You can use the PyQuery class to load an xml document from a string, a lxml document, from a file or from an url:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pyquery</span> <span class="kn">import</span> <span class="n">PyQuery</span> <span class="k">as</span> <span class="n">pq</span> <span class="gp">>>> </span><span class="kn">from</span> <span class="nn">lxml</span> <span class="kn">import</span> <span class="n">etree</span> <span class="gp">>>> </span><span class="kn">import</span> <span class="nn">urllib</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">pq</span><span class="p">(</span><span class="s">"<html></html>"</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">pq</span><span class="p">(</span><span class="n">etree</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="s">"<html></html>"</span><span class="p">))</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">pq</span><span class="p">(</span><span class="n">url</span><span class="o">=</span><span class="s">'http://google.com/'</span><span class="p">)</span> <span class="gp">>>> </span><span class="c"># d = pq(url='http://google.com/', opener=lambda url, **kw: urllib.urlopen(url).read())</span> <span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">pq</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="n">path_to_html_file</span><span class="p">)</span> </pre></div> </div> <p>Now d is like the $ in jquery:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">"#hello"</span><span class="p">)</span> <span class="go">[<p#hello.hello>]</span> <span class="gp">>>> </span><span class="n">p</span> <span class="o">=</span> <span class="n">d</span><span class="p">(</span><span class="s">"#hello"</span><span class="p">)</span> <span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">html</span><span class="p">())</span> <span class="go">Hello world !</span> <span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">html</span><span class="p">(</span><span class="s">"you know <a href='http://python.org/'>Python</a> rocks"</span><span class="p">)</span> <span class="go">[<p#hello.hello>]</span> <span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">html</span><span class="p">())</span> <span class="go">you know <a href="http://python.org/">Python</a> rocks</span> <span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">text</span><span class="p">())</span> <span class="go">you know Python rocks</span> </pre></div> </div> <p>You can use some of the pseudo classes that are available in jQuery but that are not standard in css such as :first :last :even :odd :eq :lt :gt :checked :selected :file:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p:first'</span><span class="p">)</span> <span class="go">[<p#hello.hello>]</span> </pre></div> </div> </div> <div class="section" id="full-documentation"> <h1>Full documentation<a class="headerlink" href="#full-documentation" title="Permalink to this headline">露</a></h1> <div class="toctree-wrapper compound"> <ul> <li class="toctree-l1"><a class="reference internal" href="attributes.html">Attributes</a></li> <li class="toctree-l1"><a class="reference internal" href="css.html">CSS</a></li> <li class="toctree-l1"><a class="reference internal" href="manipulating.html">Manipulating</a></li> <li class="toctree-l1"><a class="reference internal" href="traversing.html">Traversing</a></li> <li class="toctree-l1"><a class="reference internal" href="api.html"><tt class="docutils literal"><span class="pre">pyquery</span></tt> – PyQuery complete API</a></li> <li class="toctree-l1"><a class="reference internal" href="scrap.html">Scraping</a></li> <li class="toctree-l1"><a class="reference internal" href="ajax.html"><tt class="docutils literal"><span class="pre">pyquery.ajax</span></tt> – PyQuery AJAX extension</a></li> <li class="toctree-l1"><a class="reference internal" href="tips.html">Tips</a></li> <li class="toctree-l1"><a class="reference internal" href="testing.html">Testing</a></li> <li class="toctree-l1"><a class="reference internal" href="future.html">Future</a></li> <li class="toctree-l1"><a class="reference internal" href="changes.html">News</a></li> </ul> </div> </div> <div class="section" id="more-documentation"> <h1>More documentation<a class="headerlink" href="#more-documentation" title="Permalink to this headline">露</a></h1> <p>First there is the Sphinx documentation <a class="reference external" href="http://packages.python.org/pyquery/">here</a>. Then for more documentation about the API you can use the <a class="reference external" href="http://docs.jquery.com/">jquery website</a>. The reference I’m now using for the API is ... the <a class="reference external" href="http://colorcharge.com/wp-content/uploads/2007/12/jquery12_colorcharge.png">color cheat sheet</a>. Then you can always look at the <a class="reference external" href="https://github.com/gawel/pyquery">code</a>.</p> </div> <div class="section" id="indices-and-tables"> <h1>Indices and tables<a class="headerlink" href="#indices-and-tables" title="Permalink to this headline">露</a></h1> <ul class="simple"> <li><a class="reference internal" href="genindex.html"><em>Index</em></a></li> <li><a class="reference internal" href="py-modindex.html"><em>Module Index</em></a></li> <li><a class="reference internal" href="search.html"><em>Search Page</em></a></li> </ul> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="#">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">pyquery: a jquery-like library for python</a></li> <li><a class="reference internal" href="#quickstart">Quickstart</a></li> <li><a class="reference internal" href="#full-documentation">Full documentation</a><ul> </ul> </li> <li><a class="reference internal" href="#more-documentation">More documentation</a></li> <li><a class="reference internal" href="#indices-and-tables">Indices and tables</a></li> </ul> <h4>Next topic</h4> <p class="topless"><a href="attributes.html" title="next chapter">Attributes</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="_sources/index.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="py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="attributes.html" title="Attributes" >next</a> |</li> <li><a href="#">pyquery 1.2.4 documentation</a> »</li> </ul> </div> <div class="footer"> © Copyright 2012, Olivier Lauzanne. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>