pyquery – PyQuery complete API
选择器基本支持jQuery用法
- class
-
The main class
- class
-
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>']
Fn-
Alias for
add_class()
PyQuery.addClass(value)-
Add a css class to elements:
>>> d = PyQuery('<div></div>') >>> d.add_class('myclass') [<div.myclass>] >>> d.addClass('myclass') [<div.myclass>]
PyQuery.add_class(value)-
add value after nodes
PyQuery.after(value)-
append value to each nodes
PyQuery.append(value)-
Alias for
append_to()
PyQuery.appendTo(value)-
append nodes to value
PyQuery.append_to(value)-
Return the url of current html document or None if not available.
PyQuery.base_url-
insert value before nodes
PyQuery.before(value)-
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.children(selector=None)-
return a copy of nodes
PyQuery.clone()-
>>> 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.closest(selector=None)-
Return contents (with text nodes):
>>> d = PyQuery('hello <b>bold</b>') >>> d.contents() ['hello ', <Element b at ...>]
PyQuery.contents()-
apply func on each nodes
PyQuery.each(func)-
remove nodes content
PyQuery.empty()-
return the xml encoding of the root element
PyQuery.encoding-
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.end()-
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.eq(index)-
Extend with anoter PyQuery object
PyQuery.extend(other)-
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.filter(selector)-
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.find(selector)-
Alias for
has_class()
PyQuery.hasClass(name)-
Return True if element has class:
>>> d = PyQuery('<div class="myclass"></div>') >>> d.has_class('myclass') True >>> d.hasClass('myclass') True
PyQuery.has_class(name)-
set/get height of element
PyQuery.height(value=<NoDefault>)-
remove display:none to elements style
>>> print(PyQuery('<div style="display:none;"/>').hide()) <div style="display: none"/>
PyQuery.hide()-
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.html(value=<NoDefault>, **kwargs)-
Alias for
insert_after()
PyQuery.insertAfter(value)-
Alias for
insert_before()
PyQuery.insertBefore(value)-
insert nodes after value
PyQuery.insert_after(value)-
insert nodes before value
PyQuery.insert_before(value)-
Returns True if selector matches at least one current element, else False:
>>> d = PyQuery('<p class="hello"><span>Hi</span></p><p>Bye</p>') >>> d('p').eq(0).is_('.hello') True>>> d('p').eq(0).is_('span') False>>> d('p').eq(1).is_('.hello') False
PyQuery.is_(selector)-
Iter over elements. Return PyQuery objects:
>>> d = PyQuery('<div><span>foo</span><span>bar</span></div>') >>> [i.text() for i in d.items('span')] ['foo', 'bar'] >>> [i.text() for i in d('span').items()] ['foo', 'bar'] >>> list(d.items('a')) == list(d('a').items()) True
PyQuery.items(selector=None)-
Make all links absolute.
PyQuery.make_links_absolute(base_url=None)-
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:
>>> d = PyQuery('<p class="hello">Hi there</p><p>Bye</p><br />') >>> d('p').map(lambda i, e: PyQuery(e).text()) ['Hi there', 'Bye'] >>> d('p').map(lambda i, e: len(PyQuery(this).text())) [8, 3] >>> d('p').map(lambda i, e: PyQuery(this).text().split()) ['Hi', 'there', 'Bye']
PyQuery.map(func)-
Alias for
next_all()
PyQuery.nextAll(selector=None)-
>>> h = '<span><p class="hello">Hi</p><p>Bye</p><img scr=""/></span>' >>> d = PyQuery(h) >>> d('p:last').next_all() [<img>] >>> d('p:last').nextAll() [<img>]
PyQuery.next_all(selector=None)-
Return elements that don’t match the given selector:
>>> d = PyQuery('<p class="hello">Hi</p><p>Bye</p><div></div>') >>> d('p').not_('.hello') [<p>]
PyQuery.not_(selector)-
Alias for
outer_html()
PyQuery.outerHtml()-
Get the html representation of the first selected element:
>>> d = PyQuery('<div><span class="red">toto</span> rocks</div>') >>> print(d('span')) <span class="red">toto</span> rocks >>> print(d('span').outer_html()) <span class="red">toto</span> >>> print(d('span').outerHtml()) <span class="red">toto</span> >>> S = PyQuery('<p>Only <b>me</b> & myself</p>') >>> print(S('b').outer_html()) <b>me</b>
PyQuery.outer_html()-
>>> d = PyQuery('<span><p class="hello">Hi</p><p>Bye</p></span>') >>> d('p').parents() [<span>] >>> d('.hello').parents('span') [<span>] >>> d('.hello').parents('p') []
PyQuery.parents(selector=None)-
prepend value to nodes
PyQuery.prepend(value)-
Alias for
prepend_to()
PyQuery.prependTo(value)-
prepend nodes to value
PyQuery.prepend_to(value)-
Alias for
prev_all()
PyQuery.prevAll(selector=None)-
>>> h = '<span><p class="hello">Hi</p><p>Bye</p><img scr=""/></span>' >>> d = PyQuery(h) >>> d('p:last').prev_all() [<p.hello>] >>> d('p:last').prevAll() [<p.hello>]
PyQuery.prev_all(selector=None)-
Remove nodes:
>>> h = '<div>Maybe <em>she</em> does <strong>NOT</strong> know</div>' >>> d = PyQuery(h) >>> d('strong').remove() [<strong>] >>> print(d) <div>Maybe <em>she</em> does know</div>
PyQuery.remove(expr=<NoDefault>)-
Alias for
remove_attr()
PyQuery.removeAttr(name)-
Alias for
remove_class()
PyQuery.removeClass(value)-
Remove an attribute:
>>> d = PyQuery('<div id="myid"></div>') >>> d.remove_attr('id') [<div>] >>> d.removeAttr('id') [<div>]
PyQuery.remove_attr(name)-
Remove a css class to elements:
>>> d = PyQuery('<div class="myclass"></div>') >>> d.remove_class('myclass') [<div>] >>> d.removeClass('myclass') [<div>]
PyQuery.remove_class(value)-
Remove all namespaces:
>>> doc = PyQuery('<foo xmlns="http://example.com/foo"></foo>') >>> doc [<{http://example.com/foo}foo>] >>> doc.remove_namespaces() [<foo>]
PyQuery.remove_namespaces()-
Alias for
replace_all()
PyQuery.replaceAll(expr)-
Alias for
replace_with()
PyQuery.replaceWith(value)-
replace nodes by expr
PyQuery.replace_all(expr)-
replace nodes by value:
>>> doc = PyQuery("<html><div /></html>") >>> node = PyQuery("<span />") >>> child = doc.find('div') >>> child.replace_with(node)[<div>] >>> print(doc) <html><span/></html>
PyQuery.replace_with(value)-
return the xml root element
PyQuery.root-
add display:block to elements style
>>> print(PyQuery('<div />').show()) <div style="display: block"/>
PyQuery.show()-
>>> h = '<span><p class="hello">Hi</p><p>Bye</p><img scr=""/></span>' >>> d = PyQuery(h) >>> d('.hello').siblings() [<p>, <img>] >>> d('.hello').siblings('img') [<img>]
PyQuery.siblings(selector=None)-
Get or set the text representation of sub nodes.
Get the text value:
>>> doc = PyQuery('<div><span>toto</span><span>tata</span></div>') >>> print(doc.text()) toto tataSet the text value:
>>> doc.text('Youhou !') [<div>] >>> print(doc) <div>Youhou !</div>
PyQuery.text(value=<NoDefault>)-
Alias for
toggle_class()
PyQuery.toggleClass(value)-
Toggle a css class to elements
>>> d = PyQuery('<div></div>') >>> d.toggle_class('myclass') [<div.myclass>] >>> d.toggleClass('myclass') [<div>]
PyQuery.toggle_class(value)-
Set the attribute value:
>>> d = PyQuery('<input />') >>> d.val('Youhou') [<input>]Get the attribute value:
>>> d.val() 'Youhou'
PyQuery.val(value=<NoDefault>)-
set/get width of element
PyQuery.width(value=<NoDefault>)-
A string of HTML that will be created on the fly and wrapped around each target:
>>> d = PyQuery('<span>youhou</span>') >>> d.wrap('<div></div>') [<div>] >>> print(d) <div><span>youhou</span></div>
PyQuery.wrap(value)-
Alias for
wrap_all()
PyQuery.wrapAll(value)-
Wrap all the elements in the matched set into a single wrapper element:
>>> d = PyQuery('<div><span>Hey</span><span>you !</span></div>') >>> print(d('span').wrap_all('<div id="wrapper"></div>')) <div id="wrapper"><span>Hey</span><span>you !</span></div> >>> d = PyQuery('<div><span>Hey</span><span>you !</span></div>') >>> print(d('span').wrapAll('<div id="wrapper"></div>')) <div id="wrapper"><span>Hey</span><span>you !</span></div>
PyQuery.wrap_all(value)-
Remove xhtml namespace:
>>> doc = PyQuery( ... '<html xmlns="http://www.w3.org/1999/xhtml"></html>') >>> doc [<{http://www.w3.org/1999/xhtml}html>] >>> doc.xhtml_to_html() [<html>]
项目中的使用str(PQ)和PQ.outer_html()都会将部分便签由<tag></tag>变为<tag />,神奇的是有些标签变为这样形式后,
这样的字符串浏览器会解析不出来。'<!DOCTYPE html>'字符串会在变为PQ对象后自动剔除了。
PyQuery.xhtml_to_html() -
pyquery.pyquery.PyQuery(*args, **kwargs)