How do I match contents of an element in XPath (lxml)?

后端 未结 1 508
生来不讨喜
生来不讨喜 2020-12-30 07:10

I want to parse HTML with lxml using XPath expressions. My problem is matching for the contents of a tag:

For example given the



        
相关标签:
1条回答
  • 2020-12-30 07:41

    I would try with:

    .//a[text()='Example']

    using xpath() method:

    tree.xpath(".//a[text()='Example']")[0].tag
    

    If case you would like to use iterfind(), findall(), find(), findtext(), keep in mind that advanced features like value comparison and functions are not available in ElementPath.

    lxml.etree supports the simple path syntax of the find, findall and findtext methods on ElementTree and Element, as known from the original ElementTree library (ElementPath). As an lxml specific extension, these classes also provide an xpath() method that supports expressions in the complete XPath syntax, as well as custom extension functions.

    0 讨论(0)
提交回复
热议问题