class pyquery.pyquery.PyQuery(*args, **kwargs)
类pyquery.pyquery.PyQuery (* args,** kwargs )
Fn 类
构建自定义函数(如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>']
添加一个css类到元素:
PyQuery.addClass(value)
>>> d = PyQuery('<div></div>')
>>> d.addClass('myclass')
[<div.myclass>]
PyQuery.append(value)
PyQuery.append(value)
PyQuery.base_url
PyQuery.before(value)
PyQuery.children(value)
>>> 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()
PyQuery.closest(selector=None)
>>> 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()
>>> d = PyQuery('hello <b>bold</b>')
>>> d.contents()
['hello ', <Element b at ...>]
PyQuery.each(func)
PyQuery.empty()
PyQuery.encoding
PyQuery.end
>>> 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)
>>> 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)
PyQuery.filter(selector)
>>> 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)
>>> 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)
>>> d = PyQuery('<div class="myclass"></div>')
>>> d.hasClass('myclass')
True
PyQuery.height(值=<NoDeafault>)
PyQuery.hide()
>>> print(PyQuery('<div style="display:none;"/>').hide())
<div style="display: none"/>
1.获取文本值:
PyQuery.html(value=<NoDefault>, **kwargs)
>>> d = PyQuery('<div><span>toto</span></div>')
>>> print(d.html())
<span>toto</span>
2.额外的参数传递给lxml.etree.tostring:
>>> d = PyQuery('<div><span></span></div>')
>>> print(d.html())
<span/>
>>> print(d.html(method='html'))
<span></span>
3.设置文本值:
>>> d.html('<span>Youhou !</span>')
[<div>]
>>> print(d)
<div><span>Youhou !</span></div>
PyQuery.insertAfter(value)
PyQuery.insertBefore(value)
PyQuery.is_(selector)
>>> 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)
>>> 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']
PyQuery.make_links_absolute(base_url=None)
func应该有两个参数 - '索引'和'元素'。func中的元素也可以被称为'this':
PyQuery.map(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.nextAll(selector=None)
>>> h = '<span><p class="hello">Hi</p><p>Bye</p><img scr=""/></span>'
>>> d = PyQuery(h)
>>> d('p:last').nextAll()
[<img>]
PyQuery.not_(selector)
>>> d = PyQuery('<p class="hello">Hi</p><p>Bye</p><div></div>')
>>> d('p').not_('.hello')
[<p>]
PyQuery.outerHtml()
>>> d = PyQuery('<div><span class="red">toto</span> rocks</div>')
>>> print(d('span'))
<span class="red">toto</span> rocks
>>> print(d('span').outerHtml())
<span class="red">toto</span>
>>> S = PyQuery('<p>Only <b>me</b> & myself</p>')
>>> print(S('b').outerHtml())
<b>me</b>
PyQuery.parents(selector=None)[source]
>>> 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.prepend(value)
PyQuery.prependTo(value)
PyQuery.prevAll(selector=None)
>>> h = '<span><p class="hello">Hi</p><p>Bye</p><img scr=""/></span>'
>>> d = PyQuery(h)
>>> d('p:last').prevAll()
[<p.hello>]
PyQuery.remove(expr=<NoDefault>)
>>> 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.removeAttr(name)
>>> d = PyQuery('<div id="myid"></div>')
>>> d.removeAttr('id')
[<div>]
PyQuery.removeClass(value)
>>> d = PyQuery('<div class="myclass"></div>')
>>> d.removeClass('myclass')
[<div>]
PyQuery.remove_namespaces()
>>> doc = PyQuery('<foo xmlns="http://example.com/foo"></foo>')
>>> doc
[<{http://example.com/foo}foo>]
>>> doc.remove_namespaces()
[<foo>]
PyQuery.replaceAll(expr)
PyQuery.replaceWith(value)
PyQuery.root
PyQuery.show()
>>> print(PyQuery('<div />').show())
<div style="display: block"/>
PyQuery.siblings(selector=None)
>>> 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.text(value=<NoDefault>)
1.获取文本值
>>> doc = PyQuery('<div><span>toto</span><span>tata</span></div>')
>>> print(doc.text())
toto tata
2.设置文本值
>>> doc.text('Youhou !')
[<div>]
>>> print(doc)
<div>Youhou !</div>
PyQuery.toggleClass(value)
>>> d = PyQuery('<div></div>')
>>> d.toggleClass('myclass')
[<div.myclass>]
PyQuery.val(value=<NoDefault>)
1.设置属性值:>>> d = PyQuery('<input />')
>>> d.val('Youhou')
[<input>]
2.获取属性值:
>>> d.val()
'Youhou'
PyQuery.width(value=<NoDefault>)
PyQuery.wrap(value)
>>> d = PyQuery('<span>youhou</span>')
>>> d.wrap('<div></div>')
[<div>]
>>> print(d)
<div><span>youhou</span></div>
PyQuery.wrapAll(value)
>>> 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
>>> doc = PyQuery(
... '<html xmlns="http://www.w3.org/1999/xhtml"></html>')
>>> doc
[<{http://www.w3.org/1999/xhtml}html>]
>>> doc.remove_namespaces()
[<html>]
借鉴文档翻译,英文文档地址:http://https://pythonhosted.org/pyquery/api.html
中间部分不准确,使用时请注意!