find() vs items()
概述
PyQuery.find()
:返回的是lxml
的element
的generator
PyQuery.items()
:返回的是PyQuery
的generator
详解
- 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.HtmlElement
的generator
- 官网文档:pyquery.pyquery.PyQuery.find
- 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.PyQuery
的generator
- 官网文档:pyquery.pyquery.PyQuery.items
举例
<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'>