How do I select all of the tr's except the last two tr's

2.6k Views Asked by At

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 have 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!

2

There are 2 best solutions below

0
On BEST ANSWER
result = doc.xpath("//tr")[0:-2]

Should do the trick.

2
On

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.