pyquery – PyQuery complete API

class pyquery.pyquery.PyQuery(*args, **kwargs)[source]

The main class

class Fn[source]

Hook for defining custom function (like the jQuery.fn):

>>> fn = lambda: this.map(lambda i, el: PyQuery(this).outerHtml())
>>> PyQuery.fn.listOuterHtml = fn
>>> S = PyQuery(
...   '<ol>   <li>Coffee</li>   <li>Tea</li>   <li>Milk</li>   </ol>')
>>> S('li').listOuterHtml()
['<li>Coffee</li>', '<li>Tea</li>', '<li>Milk</li>']
PyQuery.addClass(value)[source]

Add a css class to elements:

>>> d = PyQuery('<div></div>')
>>> d.addClass('myclass')
[<div.myclass>]
PyQuery.after(value)[source]

add value after nodes

PyQuery.append(value)[source]

append value to each nodes

PyQuery.appendTo(value)[source]

append nodes to value

PyQuery.base_url[source]

Return the url of current html document or None if not available.

PyQuery.before(value)[source]

insert value before nodes

PyQuery.children(selector=None)[source]

Filter elements that are direct children of self using optional selector:

>>> d = PyQuery('<span><p class="hello">Hi</p><p>Bye</p></span>')
>>> d
[<span>]
>>> d.children()
[<p.hello>, <p>]
>>> d.children('.hello')
[<p.hello>]
PyQuery.clone()[source]

return a copy of nodes

PyQuery.closest(selector=None)[source]
>>> d = PyQuery(
...  '<div class="hello"><p>This is a '
...  '<strong class="hello">test</strong></p></div>')
>>> d('strong').closest('div')
[<div.hello>]
>>> d('strong').closest('.hello')
[<strong.hello>]
>>> d('strong').closest('form')
[]
PyQuery.contents()[source]

Return contents (with text nodes):

>>> d = PyQuery('hello <b>bold</b>')
>>> d.contents()  
['hello ', <Element b at ...>]
PyQuery.each(func)[source]

apply func on each nodes

PyQuery.empty()[source]

remove nodes content

PyQuery.encoding[source]

return the xml encoding of the root element

PyQuery.end()[source]

Break out of a level of traversal and return to the parent level.

>>> m = '<p><span><em>Whoah!</em></span></p><p><em> there</em></p>'
>>> d = PyQuery(m)
>>> d('p').eq(1).find('em').end().end()
[<p>, <p>]
PyQuery.eq(index)[source]

Return PyQuery of only the element with the provided index:

>>> d = PyQuery('<p class="hello">Hi</p><p>Bye</p><div></div>')
>>> d('p').eq(0)
[<p.hello>]
>>> d('p').eq(1)
[<p>]
>>> d('p').eq(2)
[]
PyQuery.extend(other)[source]

Extend with anoter PyQuery object

PyQuery.filter(selector)[source]

Filter elements in self using selector (string or function):

>>> d = PyQuery('<p class="hello">Hi</p><p>Bye</p>')
>>> d('p')
[<p.hello>, <p>]
>>> d('p').filter('.hello')
[<p.hello>]
>>> d('p').filter(lambda i: i == 1)
[<p>]
>>> d('p').filter(lambda i: PyQuery(this).text() == 'Hi')
[<p.hello>]
>>> d('p').filter(lambda i, this: PyQuery(this).text() == 'Hi')
[<p.hello>]
PyQuery.find(selector)[source]

Find elements using selector traversing down from self:

>>> m = '<p><span><em>Whoah!</em></span></p><p><em> there</em></p>'
>>> d = PyQuery(m)
>>> d('p').find('em')
[<em>, <em>]
>>> d('p').eq(1).find('em')
[<em>]
PyQuery.hasClass(name)[source]

Return True if element has class:

>>> d = PyQuery('<div class="myclass"></div>')
>>> d.hasClass('myclass')
True
PyQuery.height(value=<NoDefault>)[source]

set/get height of element

PyQuery.hide()[source]

remove display:none to elements style

>>> print(PyQuery('<div style="display:none;"/>').hide())
<div style="display: none"/>
PyQuery.html(value=<NoDefault>, **kwargs)[source]

Get or set the html representation of sub nodes.

Get the text value:

>>> d = PyQuery('<div><span>toto</span></div>')
>>> print(d.html())
<span>toto</span>

Extra args are passed to lxml.etree.tostring:

>>> d = PyQuery('<div><span></span></div>')
>>> print(d.html())
<span/>
>>> print(d.html(method='html'))
<span></span>

Set the text value:

>>> d.html('<span>Youhou !</span>')
[<div>]
>>> print(d)
<div><span>Youhou !</span></div>
PyQuery.insertAfter(value)[source]

insert nodes after value

PyQuery.insertBefore(value)[source]

insert nodes before value

PyQuery.is_(selector)[source]

Returns True if selector matches at least one current element, else False:

>>> d = PyQuery('<p class="hello">Hi</p><p>Bye</p><div></div>')
>>> d('p').eq(0).is_('.hello')
True
>>> d('p').eq(1).is_('.hello')
False
PyQuery.items(selector=None)[source]

Iter over elements. Return PyQuery objects:

>>> d = PyQuery('<div><span>foo</span><span>bar</span></div>')
>>> [i.text() for i in d.items('span')]