python - how to get a few items from the set? -
please 2 of 3 li. use following script:
import lxml.html doc = lxml.html.document_fromstring(""" <ul> <li>111</li> <li>222</li> <li>333</li> </ul> """) lis = doc.xpath('//li') = 0 result = [] elem in lis: result.append(elem) = + 1 if > 1: break print(result)
but need sample made no xpath-expression , cycle, xpath-expression.
you can use position()
reduce xpath result set, eg:
result = doc.xpath('//li[position() <= 2]') # [<element li @ 0x7f7728684530>, <element li @ 0x7f7728684590>]
Comments
Post a Comment