PyQuery 文档标注
程序员文章站
2022-05-02 18:07:12
...
PyQuery complete API
用来筛选
- PyQuery.eq(index)[source]
>>> 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.find(selector)[source]
>>> 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.filter(selector)[source]
>>> 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.items(selector=None)[source]
>>> 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.children(selector=None)[source]
>>> 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.parents(selector=None)
>>> 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.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.end()[source]
>>> 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.nextAll(selector=None)[source]
>>> h = '<span><p class="hello">Hi</p><p>Bye</p><img scr=""/></span>'
>>> d = PyQuery(h)
>>> d('p:last').nextAll()
[<img>]
# 注意这里的:last,用来选择最后一个匹配
- PyQuery.prevAll(selector=None)[source]
>>> h = '<span><p class="hello">Hi</p><p>Bye</p><img scr=""/></span>'
>>> d = PyQuery(h)
>>> d('p:last').prevAll()
[<p.hello>]
- PyQuery.outerHtml()[source]
>>> 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>
# 注意,取span会取到后面的text,outerHtml就只取span
- PyQuery.siblings(selector=None)[source]
>>> 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.hasClass(name)[source]
>>> d = PyQuery('<div class="myclass"></div>')
>>> d.hasClass('myclass')
True
- PyQuery.is_(selector)[source]
>>> 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.not_(selector)[source]
>>> d = PyQuery('<p class="hello">Hi</p><p>Bye</p><div></div>')
>>> d('p').not_('.hello')
[<p>]
获取内容
- PyQuery.text(value=)[source]
>>> doc = PyQuery('<div><span>toto</span><span>tata</span></div>')
>>> print(doc.text())
toto tata
- PyQuery.val(value=)[source]
>>> d.val()
'Youhou'
- PyQuery.width(value=)[source]
- PyQuery.height(value=)[source]
注意
使用pyquery的时候,一定要注意namespace,可能找不到element。先移除doc = pq(html).remove_namespaces()