How to use regular expression in lxml xpath?

后端 未结 3 683
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 00:47

I\'m using construction like this:

doc = parse(url).getroot()
links = doc.xpath(\"//a[text()=\'some text\']\")

But I need to select all lin

3条回答
  •  醉话见心
    2020-11-30 01:25

    You can do this (although you don't need regular expressions for the example). Lxml supports regular expressions from the EXSLT extension functions. (see the lxml docs for the XPath class, but it also works for the xpath() method)

    doc.xpath("//a[re:match(text(), 'some text')]", 
            namespaces={"re": "http://exslt.org/regular-expressions"})
    

    Note that you need to give the namespace mapping, so that it knows what the "re" prefix in the xpath expression stands for.

提交回复
热议问题