Question

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!

Was it helpful?

Solution

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

Should do the trick.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top