PyQuery 文档标注

官方文档

PyQuery complete API

用来筛选

  • PyQuery.eq(index)[source]
>>> d = PyQuery('

Hi

Bye

'
) >>> d('p').eq(0) [] >>> d('p').eq(1) [

] >>> d('p').eq(2) []

  • PyQuery.find(selector)[source]
>>> m = '

Whoah!

there

'
>>> d = PyQuery(m) >>> d('p').find('em') [, ] >>> d('p').eq(1).find('em') []
  • PyQuery.filter(selector)[source]
>>> 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') []

  • PyQuery.items(selector=None)[source]
>>> d = PyQuery('
foobar
'
) >>> [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('

Hi

Bye

'
) >>> d [] >>> d.children() [,

] >>> d.children('.hello') []

  • PyQuery.parents(selector=None)
>>> d = PyQuery('

Hi

Bye

'
) >>> d('p').parents() [] >>> d('.hello').parents('span') [] >>> d('.hello').parents('p') []
  • PyQuery.closest(selector=None)[source]
>>> d = PyQuery(
...  '

This is a ' ... 'test

'
) >>> d('strong').closest('div') [] >>> d('strong').closest('.hello') [] >>> d('strong').closest('form') []
  • PyQuery.end()[source]
>>> 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>]
  • PyQuery.nextAll(selector=None)[source]
>>> h = '

Hi

Bye

'
>>> d = PyQuery(h) >>> d('p:last').nextAll() [] # 注意这里的:last,用来选择最后一个匹配
  • PyQuery.prevAll(selector=None)[source]
>>> h = '

Hi

Bye

'
>>> d = PyQuery(h) >>> d('p:last').prevAll() []
  • PyQuery.outerHtml()[source]
>>> 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会取到后面的textouterHtml就只取span
  • PyQuery.siblings(selector=None)[source]
>>> h = '

Hi

Bye

'
>>> d = PyQuery(h) >>> d('.hello').siblings() [

, ] >>> d('.hello').siblings('img') []

用来判断

  • PyQuery.hasClass(name)[source]
>>> d = PyQuery('
'
) >>> d.hasClass('myclass') True
  • PyQuery.is_(selector)[source]
>>> d = PyQuery('

Hi

Bye

'
) >>> d('p').eq(0).is_('.hello') True >>> d('p').eq(1).is_('.hello') False
  • PyQuery.not_(selector)[source]
>>> d = PyQuery('<p class="hello">Hip><p>Byep><div>div>')
>>> d('p').not_('.hello')
[<p>]

获取内容

  • PyQuery.text(value=)[source]
>>> doc = PyQuery('
tototata
'
) >>> 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()

你可能感兴趣的:(python)