In lxml, I'm using xpath to select all of the tr's in a table (that has varying number of rows) except for the last two rows which contain gibberish.
Is there a pattern match that excludes the last two rows? I was looking through xpath tutorials and apparently there is an "except" operator and also a "last()," but can't seem to get my code working.
So far I h开发者_高级运维ave this. What do I add to this pattern to make it exclude the last two rows? The main problem is the number of tr's vary.
result = doc.xpath("//tr")
I guess I could turn this into a list and just remove the last two elements, but is there any easier/elegant solution?
Thanks in advance!
Use:
expressionSelectingTheTable/tr[not(position() > last() -2)]
where expressionSelectingTheTable
should be substituted with a specific XPath expression that selects the table, for which the question is being asked (such as //table[@id='foo']
)
This single XPath expression selects all tr
children of the table
parent, whose position is not one of the last two.
result = doc.xpath("//tr")[0:-2]
Should do the trick.
精彩评论