官方文档
>>> d = PyQuery('Hi
Bye
')
>>> d('p').eq(0)
[]
>>> d('p').eq(1)
[]
>>> d('p').eq(2)
[]
>>> m = 'Whoah!
there
'
>>> d = PyQuery(m)
>>> d('p').find('em')
[, ]
>>> d('p').eq(1).find('em')
[]
>>> d = PyQuery('Hi
Bye
')
>>> d('p')
[, ]
>>> d('p').filter('.hello')
[
]
>>> d('p').filter(lambda i: i == 1)
[]
>>> d('p').filter(lambda i: PyQuery(this).text() == 'Hi')
[
]
>>> d('p').filter(lambda i, this: PyQuery(this).text() == 'Hi')
[]
>>> d = PyQuery('foobar')
>>> [i.text() for i in d.items('span')]
['foo', 'bar']
>>> [i.text() for i in d('span').items()]
['foo', 'bar']
>>> d = PyQuery('Hi
Bye
')
>>> d
[]
>>> d.children()
[, ]
>>> d.children('.hello')
[
]
>>> d = PyQuery('Hi
Bye
')
>>> d('p').parents()
[]
>>> d('.hello').parents('span')
[]
>>> d('.hello').parents('p')
[]
>>> d = PyQuery(
... 'This is a '
... 'test
')
>>> d('strong').closest('div')
[]
>>> d('strong').closest('.hello')
[]
>>> d('strong').closest('form')
[]
>>> m = '<p><span><em>Whoah!em>span>p><p><em> thereem>p>'
>>> d = PyQuery(m)
>>> d('p').eq(1).find('em').end().end()
[<p>, <p>]
>>> h = 'Hi
Bye
'
>>> d = PyQuery(h)
>>> d('p:last').nextAll()
[]
# 注意这里的:last,用来选择最后一个匹配
>>> h = 'Hi
Bye
'
>>> d = PyQuery(h)
>>> d('p:last').prevAll()
[]
>>> d = PyQuery('toto rocks')
>>> print(d('span'))
class="red">totospan> rocks
>>> print(d('span').outerHtml())
<span class="red">totospan>
>>> S = PyQuery('Only me & myself
')
>>> print(S('b').outerHtml())
<b>meb>
# 注意,取span会取到后面的text,outerHtml就只取span
>>> h = 'Hi
Bye
'
>>> d = PyQuery(h)
>>> d('.hello').siblings()
[, ]
>>> d('.hello').siblings('img')
[]
>>> d = PyQuery('')
>>> d.hasClass('myclass')
True
>>> d = PyQuery('Hi
Bye
')
>>> d('p').eq(0).is_('.hello')
True
>>> d('p').eq(1).is_('.hello')
False
>>> d = PyQuery('<p class="hello">Hip><p>Byep><div>div>')
>>> d('p').not_('.hello')
[<p>]
>>> doc = PyQuery('tototata')
>>> print(doc.text())
toto tata
>>> d.val()
'Youhou'
使用pyquery的时候,一定要注意namespace,可能找不到element。先移除doc = pq(html).remove_namespaces()