find() vs items()

概述

  • PyQuery.find():返回的是lxmlelementgenerator
  • PyQuery.items():返回的是PyQuerygenerator

详解

  • find
    • 官网文档:pyquery.pyquery.PyQuery.find

      PyQuery.find(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>]
      
    • -> find()返回的是element元素=lxml.html.HtmlElementgenerator
  • items
    • 官网文档:pyquery.pyquery.PyQuery.items

      PyQuery.items(selector=None)

      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']
      
    • -> items()返回的是PyQuery=pyquery.pyquery.PyQuerygenerator

举例

    <ul class="rank-list-ul" 0>

      <li id="s3170">
...
      </li>

      <li id="s692">
...
      </li>

find()

如果是find()

carSeriesDocGenerator = merchantRankDoc.find("li[id*='s']")
print("type(carSeriesDocGenerator)=%s" % type(carSeriesDocGenerator))

则返回的是PyQuery

type(carSeriesDocGenerator)=<class 'pyquery.pyquery.PyQuery'>

然后generator转换成list后:

carSeriesDocList = list(carSeriesDocGenerator)
for curSeriesIdx, eachCarSeriesDoc in enumerate(carSeriesDocList):
    print("type(eachCarSeriesDoc)=%s" % type(eachCarSeriesDoc))

每个元素是:lxml.html.HtmlElement

type(eachCarSeriesDoc)=<class 'lxml.html.HtmlElement'>

items()

如果换成items()

carSeriesDocGenerator = merchantRankDoc.items("li[id*='s']")
print("type(carSeriesDocGenerator)=%s" % type(carSeriesDocGenerator))

则返回的是generator

type(carSeriesDocGenerator)=<class 'generator'>

然后generator转换成list后:

carSeriesDocList = list(carSeriesDocGenerator)
for curSeriesIdx, eachCarSeriesDoc in enumerate(carSeriesDocList):
    print("type(eachCarSeriesDoc)=%s" % type(eachCarSeriesDoc))

每个元素是:pyquery.pyquery.PyQuery

type(eachCarSeriesDoc)=<class 'pyquery.pyquery.PyQuery'>

results matching ""

    No results matching ""