Python PyQuery Module – Complete API & Commands
Pyquery is a module that allows you to parse HTML. The great thing about this module is that you use the Jquery selector and functions when you do this. If you know a little bit of jQuery, you can use this module to get the desired part of an Internet page from the source very easily.
How to Install PyQuery Module
Give the command to install
1 | sudo pip install pyquery; |
Learn Python PyQuery Module
Since the installation is ok, we can use it. Let’s get the title of the site we’re linking to.
1 2 3 4 5 6 7 | from pyquery import PyQuery k = PyQuery("https://opensourceprojects.org") print k('title').html() #Output; Open Source Projects For Beginners |
As you can see, we can use the jQuery properties on the K object. We can delete the property.
1 2 3 4 5 | k = PyQuery('<div class="sinif"><a href="">URL OSP</a></div>') k.remove_attr('class') print k #Output; <div><a href="">URL</a></div> |
We deleted the class property. We can select an object from the element, search for an object, change the contents of the object we find. Let’s do it all at once.
1 2 3 4 5 | from pyquery import PyQuery k = PyQuery("https://opensourceprojects.org") print k('.commentcount').eq(0).find("a").append(" Changed") #Output; <a href="https://opensourceprojects.org" class="comment-link" title="Open Source Projects"><span class="icon comments"/> 1 Comment</a> |
For example, let’s get links to the links in the menu section of the site we’re connecting to.
1 2 3 4 5 | for i in k(".menu").find("ul").find("li").items("a"): print i.attr("href") #Output http://opensourceprojects.org/contact/ http://opensourceprojects.org/about/ |
As you can see, if you have a bit of jQuery knowledge, this module is really easy to parse HTML.
pyquery — PyQuery complete API
class pyquery.pyquery.PyQuery(*args, **kwargs)
The main class
class Fn
- Hook for defining custom function (like the jQuery.fn):123456<span class="gp">>>> </span><span class="n">fn</span> <span class="o">=</span> <span class="k">lambda</span><span class="p">:</span> <span class="n">this</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">i</span><span class="p">,</span> <span class="n">el</span><span class="p">:</span> <span class="n">PyQuery</span><span class="p">(</span><span class="n">this</span><span class="p">)</span><span class="o">.</span><span class="n">outerHtml</span><span class="p">())</span><span class="gp">>>> </span><span class="n">PyQuery</span><span class="o">.</span><span class="n">fn</span><span class="o">.</span><span class="n">listOuterHtml</span> <span class="o">=</span> <span class="n">fn</span><span class="gp">>>> </span><span class="n">S</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="gp">... </span> <span class="s">'<ol> <li>Coffee</li> <li>Tea</li> <li>Milk</li> </ol>'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">S</span><span class="p">(</span><span class="s">'li'</span><span class="p">)</span><span class="o">.</span><span class="n">listOuterHtml</span><span class="p">()</span><span class="go">['<li>Coffee</li>', '<li>Tea</li>', '<li>Milk</li>']</span>
PyQuery.addClass(value)
- Add a css class to elements:123<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<div></div>'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">addClass</span><span class="p">(</span><span class="s">'myclass'</span><span class="p">)</span><span class="go">[<div.myclass>]</span>
PyQuery.after(value)
- add value after nodes
PyQuery.append(value)
- append value to each nodes
PyQuery.appendTo(value)
- append nodes to value
PyQuery.base_url
- Return the url of current html document or None if not available.
PyQuery.before(value)
- insert value before nodes
PyQuery.children(selector=None)
- Filter elements that are direct children of self using optional selector:1234567<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<span><p class="hello">Hi</p><p>Bye</p></span>'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="go">[<span>]</span><span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">children</span><span class="p">()</span><span class="go">[<p.hello>, <p>]</span><span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">children</span><span class="p">(</span><span class="s">'.hello'</span><span class="p">)</span><span class="go">[<p.hello>]</span>
PyQuery.clone()
- return a copy of nodes
PyQuery.closest(selector=None)
- 123456789<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="gp">... </span> <span class="s">'<div class="hello"><p>This is a '</span><span class="gp">... </span> <span class="s">'<strong class="hello">test</strong></p></div>'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'strong'</span><span class="p">)</span><span class="o">.</span><span class="n">closest</span><span class="p">(</span><span class="s">'div'</span><span class="p">)</span><span class="go">[<div.hello>]</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'strong'</span><span class="p">)</span><span class="o">.</span><span class="n">closest</span><span class="p">(</span><span class="s">'.hello'</span><span class="p">)</span><span class="go">[<strong.hello>]</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'strong'</span><span class="p">)</span><span class="o">.</span><span class="n">closest</span><span class="p">(</span><span class="s">'form'</span><span class="p">)</span><span class="go">[]</span>
PyQuery.contents()
- Return contents (with text nodes):123<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'hello <b>bold</b>'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">contents</span><span class="p">()</span><span class="go">['hello ', <Element b at ...>]</span>
PyQuery.each(func)
- apply func on each nodes
PyQuery.empty()
- remove nodes content
PyQuery.encoding
- return the xml encoding of the root element
PyQuery.end()
- Break out of a level of traversal and return to the parent level.1234<span class="gp">>>> </span><span class="n">m</span> <span class="o">=</span> <span class="s">'<p><span><em>Whoah!</em></span></p><p><em> there</em></p>'</span><span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="n">m</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="o">.</span><span class="n">eq</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s">'em'</span><span class="p">)</span><span class="o">.</span><span class="n">end</span><span class="p">()</span><span class="o">.</span><span class="n">end</span><span class="p">()</span><span class="go">[<p>, <p>]</span>
PyQuery.eq(index)
- Return PyQuery of only the element with the provided index:1234567<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<p class="hello">Hi</p><p>Bye</p><div></div>'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="o">.</span><span class="n">eq</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span><span class="go">[<p.hello>]</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="o">.</span><span class="n">eq</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="go">[<p>]</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="o">.</span><span class="n">eq</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="go">[]</span>
PyQuery.extend(other)
- Extend with anoter PyQuery object
PyQuery.filter(selector)
- Filter elements in self using selector (string or function):1234567891011<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<p class="hello">Hi</p><p>Bye</p>'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="go">[<p.hello>, <p>]</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="s">'.hello'</span><span class="p">)</span><span class="go">[<p.hello>]</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">i</span><span class="p">:</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span><span class="go">[<p>]</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">i</span><span class="p">:</span> <span class="n">PyQuery</span><span class="p">(</span><span class="n">this</span><span class="p">)</span><span class="o">.</span><span class="n">text</span><span class="p">()</span> <span class="o">==</span> <span class="s">'Hi'</span><span class="p">)</span><span class="go">[<p.hello>]</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">i</span><span class="p">,</span> <span class="n">this</span><span class="p">:</span> <span class="n">PyQuery</span><span class="p">(</span><span class="n">this</span><span class="p">)</span><span class="o">.</span><span class="n">text</span><span class="p">()</span> <span class="o">==</span> <span class="s">'Hi'</span><span class="p">)</span><span class="go">[<p.hello>]</span>
PyQuery.find(selector)
- Find elements using selector traversing down from self:123456<span class="gp">>>> </span><span class="n">m</span> <span class="o">=</span> <span class="s">'<p><span><em>Whoah!</em></span></p><p><em> there</em></p>'</span><span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="n">m</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s">'em'</span><span class="p">)</span><span class="go">[<em>, <em>]</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="o">.</span><span class="n">eq</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s">'em'</span><span class="p">)</span><span class="go">[<em>]</span>
PyQuery.hasClass(name)
- Return True if element has class:123<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<div class="myclass"></div>'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">hasClass</span><span class="p">(</span><span class="s">'myclass'</span><span class="p">)</span><span class="go">True</span>
PyQuery.height(value=<NoDefault>)
- set/get height of element
PyQuery.hide()
- remove display:none to elements style12<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">PyQuery</span><span class="p">(</span><span class="s">'<div style="display:none;"/>'</span><span class="p">)</span><span class="o">.</span><span class="n">hide</span><span class="p">())</span><span class="go"><div style="display: none"/></span>
PyQuery.html(value=<NoDefault>, **kwargs)
- Get or set the html representation of sub nodes.
Get the text value:
123<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<div><span>toto</span></div>'</span><span class="p">)</span><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">html</span><span class="p">())</span><span class="go"><span>toto</span></span>Extra args are passed to lxml.etree.tostring:
12345<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<div><span></span></div>'</span><span class="p">)</span><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">html</span><span class="p">())</span><span class="go"><span/></span><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">html</span><span class="p">(</span><span class="n">method</span><span class="o">=</span><span class="s">'html'</span><span class="p">))</span><span class="go"><span></span></span>Set the text value:
1234<span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">html</span><span class="p">(</span><span class="s">'<span>Youhou !</span>'</span><span class="p">)</span><span class="go">[<div>]</span><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span><span class="go"><div><span>Youhou !</span></div></span>
PyQuery.insertAfter(value)
- insert nodes after value
PyQuery.insertBefore(value)
- insert nodes before value
PyQuery.is_(selector)
- Returns True if selector matches at least one current element, else False:123<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<p class="hello">Hi</p><p>Bye</p><div></div>'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="o">.</span><span class="n">eq</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">is_</span><span class="p">(</span><span class="s">'.hello'</span><span class="p">)</span><span class="go">True</span>12<span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="o">.</span><span class="n">eq</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">is_</span><span class="p">(</span><span class="s">'.hello'</span><span class="p">)</span><span class="go">False</span>
PyQuery.items(selector=None)
- Iter over elements. Return PyQuery objects:12345<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<div><span>foo</span><span>bar</span></div>'</span><span class="p">)</span><span class="gp">>>> </span><span class="p">[</span><span class="n">i</span><span class="o">.</span><span class="n">text</span><span class="p">()</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">(</span><span class="s">'span'</span><span class="p">)]</span><span class="go">['foo', 'bar']</span><span class="gp">>>> </span><span class="p">[</span><span class="n">i</span><span class="o">.</span><span class="n">text</span><span class="p">()</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">d</span><span class="p">(</span><span class="s">'span'</span><span class="p">)</span><span class="o">.</span><span class="n">items</span><span class="p">()]</span><span class="go">['foo', 'bar']</span>
PyQuery.make_links_absolute(base_url=None)
- Make all links absolute.
PyQuery.map(func)
- Returns a new PyQuery after transforming current items with func.
func should take two arguments – ‘index’ and ‘element’. Elements can also be referred to as ‘this’ inside of func:
123456789<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<p class="hello">Hi there</p><p>Bye</p><br />'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">i</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="n">PyQuery</span><span class="p">(</span><span class="n">e</span><span class="p">)</span><span class="o">.</span><span class="n">text</span><span class="p">())</span><span class="go">['Hi there', 'Bye']</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">i</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="nb">len</span><span class="p">(</span><span class="n">PyQuery</span><span class="p">(</span><span class="n">this</span><span class="p">)</span><span class="o">.</span><span class="n">text</span><span class="p">()))</span><span class="go">[8, 3]</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">i</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span> <span class="n">PyQuery</span><span class="p">(</span><span class="n">this</span><span class="p">)</span><span class="o">.</span><span class="n">text</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">())</span><span class="go">['Hi', 'there', 'Bye']</span>
PyQuery.nextAll(selector=None)
- 1234<span class="gp">>>> </span><span class="n">h</span> <span class="o">=</span> <span class="s">'<span><p class="hello">Hi</p><p>Bye</p><img scr=""/></span>'</span><span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="n">h</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p:last'</span><span class="p">)</span><span class="o">.</span><span class="n">nextAll</span><span class="p">()</span><span class="go">[<img>]</span>
PyQuery.not_(selector)
- Return elements that don’t match the given selector:123<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<p class="hello">Hi</p><p>Bye</p><div></div>'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="o">.</span><span class="n">not_</span><span class="p">(</span><span class="s">'.hello'</span><span class="p">)</span><span class="go">[<p>]</span>
PyQuery.outerHtml()
- Get the html representation of the first selected element:123456789<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<div><span class="red">toto</span> rocks</div>'</span><span class="p">)</span><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">d</span><span class="p">(</span><span class="s">'span'</span><span class="p">))</span><span class="go"><span class="red">toto</span> rocks</span><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">d</span><span class="p">(</span><span class="s">'span'</span><span class="p">)</span><span class="o">.</span><span class="n">outerHtml</span><span class="p">())</span><span class="go"><span class="red">toto</span></span><span class="gp">>>> </span><span class="n">S</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<p>Only <b>me</b> & myself</p>'</span><span class="p">)</span><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">S</span><span class="p">(</span><span class="s">'b'</span><span class="p">)</span><span class="o">.</span><span class="n">outerHtml</span><span class="p">())</span><span class="go"><b>me</b></span>
PyQuery.parents(selector=None)
- 1234567<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<span><p class="hello">Hi</p><p>Bye</p></span>'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="o">.</span><span class="n">parents</span><span class="p">()</span><span class="go">[<span>]</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'.hello'</span><span class="p">)</span><span class="o">.</span><span class="n">parents</span><span class="p">(</span><span class="s">'span'</span><span class="p">)</span><span class="go">[<span>]</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'.hello'</span><span class="p">)</span><span class="o">.</span><span class="n">parents</span><span class="p">(</span><span class="s">'p'</span><span class="p">)</span><span class="go">[]</span>
PyQuery.prepend(value)
- prepend value to nodes
PyQuery.prependTo(value)
- prepend nodes to value
PyQuery.prevAll(selector=None)
- 1234<span class="gp">>>> </span><span class="n">h</span> <span class="o">=</span> <span class="s">'<span><p class="hello">Hi</p><p>Bye</p><img scr=""/></span>'</span><span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="n">h</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'p:last'</span><span class="p">)</span><span class="o">.</span><span class="n">prevAll</span><span class="p">()</span><span class="go">[<p.hello>]</span>
PyQuery.remove(expr=<NoDefault>)
- Remove nodes:123456<span class="gp">>>> </span><span class="n">h</span> <span class="o">=</span> <span class="s">'<div>Maybe <em>she</em> does <strong>NOT</strong> know</div>'</span><span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="n">h</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'strong'</span><span class="p">)</span><span class="o">.</span><span class="n">remove</span><span class="p">()</span><span class="go">[<strong>]</span><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span><span class="go"><div>Maybe <em>she</em> does know</div></span>
PyQuery.removeAttr(name)
- Remove an attribute:123<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<div id="myid"></div>'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">removeAttr</span><span class="p">(</span><span class="s">'id'</span><span class="p">)</span><span class="go">[<div>]</span>
PyQuery.removeClass(value)
- Remove a css class to elements:123<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<div class="myclass"></div>'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">removeClass</span><span class="p">(</span><span class="s">'myclass'</span><span class="p">)</span><span class="go">[<div>]</span>
PyQuery.remove_namespaces()
- Remove all namespaces:12345<span class="gp">>>> </span><span class="n">doc</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<foo xmlns="http://example.com/foo"></foo>'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">doc</span><span class="go">[<{http://example.com/foo}foo>]</span><span class="gp">>>> </span><span class="n">doc</span><span class="o">.</span><span class="n">remove_namespaces</span><span class="p">()</span><span class="go">[<foo>]</span>
PyQuery.replaceAll(expr)
- replace nodes by expr
PyQuery.replaceWith(value)
- replace nodes by value
PyQuery.root
- return the xml root element
PyQuery.show()
- add display:block to elements style12<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">PyQuery</span><span class="p">(</span><span class="s">'<div />'</span><span class="p">)</span><span class="o">.</span><span class="n">show</span><span class="p">())</span><span class="go"><div style="display: block"/></span>
PyQuery.siblings(selector=None)
- 123456<span class="gp">>>> </span><span class="n">h</span> <span class="o">=</span> <span class="s">'<span><p class="hello">Hi</p><p>Bye</p><img scr=""/></span>'</span><span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="n">h</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'.hello'</span><span class="p">)</span><span class="o">.</span><span class="n">siblings</span><span class="p">()</span><span class="go">[<p>, <img>]</span><span class="gp">>>> </span><span class="n">d</span><span class="p">(</span><span class="s">'.hello'</span><span class="p">)</span><span class="o">.</span><span class="n">siblings</span><span class="p">(</span><span class="s">'img'</span><span class="p">)</span><span class="go">[<img>]</span>
PyQuery.text(value=<NoDefault>)
- Get or set the text representation of sub nodes.
Get the text value:
123<span class="gp">>>> </span><span class="n">doc</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<div><span>toto</span><span>tata</span></div>'</span><span class="p">)</span><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">doc</span><span class="o">.</span><span class="n">text</span><span class="p">())</span><span class="go">toto tata</span>Set the text value:
1234<span class="gp">>>> </span><span class="n">doc</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="s">'Youhou !'</span><span class="p">)</span><span class="go">[<div>]</span><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span><span class="go"><div>Youhou !</div></span>
PyQuery.toggleClass(value)
- Toggle a css class to elements123<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<div></div>'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">toggleClass</span><span class="p">(</span><span class="s">'myclass'</span><span class="p">)</span><span class="go">[<div.myclass>]</span>
PyQuery.val(value=<NoDefault>)
- Set the attribute value:123<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<input />'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">val</span><span class="p">(</span><span class="s">'Youhou'</span><span class="p">)</span><span class="go">[<input>]</span>
Get the attribute value:
12<span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">val</span><span class="p">()</span><span class="go">'Youhou'</span>
PyQuery.width(value=<NoDefault>)
- set/get width of element
PyQuery.wrap(value)
- A string of HTML that will be created on the fly and wrapped around each target:12345<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<span>youhou</span>'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">wrap</span><span class="p">(</span><span class="s">'<div></div>'</span><span class="p">)</span><span class="go">[<div>]</span><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span><span class="go"><div><span>youhou</span></div></span>
PyQuery.wrapAll(value)
- Wrap all the elements in the matched set into a single wrapper element:123<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="s">'<div><span>Hey</span><span>you !</span></div>'</span><span class="p">)</span><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">d</span><span class="p">(</span><span class="s">'span'</span><span class="p">)</span><span class="o">.</span><span class="n">wrapAll</span><span class="p">(</span><span class="s">'<div id="wrapper"></div>'</span><span class="p">))</span><span class="go"><div id="wrapper"><span>Hey</span><span>you !</span></div></span>
PyQuery.xhtml_to_html()
- Remove xhtml namespace:123456<span class="gp">>>> </span><span class="n">doc</span> <span class="o">=</span> <span class="n">PyQuery</span><span class="p">(</span><span class="gp">... </span> <span class="s">'<html xmlns="http://www.w3.org/1999/xhtml"></html>'</span><span class="p">)</span><span class="gp">>>> </span><span class="n">doc</span><span class="go">[<{http://www.w3.org/1999/xhtml}html>]</span><span class="gp">>>> </span><span class="n">doc</span><span class="o">.</span><span class="n">remove_namespaces</span><span class="p">()</span><span class="go">[<html>]</span>
- Complete Api Source; https://pythonhosted.org/pyquery/api.html#module-pyquery.pyquery