Search code examples
pythonxpathlxmlexcept

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


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!


Solution

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

    Should do the trick.