I'm trying to do:
我想做的事:
for element in root.xpath('//a[@id="hypProduct_[0-9]+"]'):
How do i use [0-9]+ within an xpath element selector (lxml)? The docs state:
如何在xpath元素选择器(lxml)中使用[0-9]+ ?文档状态:
By default, XPath supports regular expressions in the EXSLT namespace:
>>> regexpNS = "http://exslt.org/regular-expressions"
>>> find = etree.XPath("//*[re:test(., '^abc$', 'i')]",
... namespaces={'re':regexpNS})
>>> root = etree.XML("<root><a>aB</a><b>aBc</b></root>")
>>> print(find(root)[0].text)
aBc
You can disable this with the boolean keyword argument regexp which defaults to True.
I didn't follow the :test stuff. Could someone explain this in context of the docs.
我没有遵循:测试内容。有人能在文档的上下文中解释一下吗?
1 个解决方案
#1
3
In your case, the expression would be:
在你的情况下,表达式是:
//a[re:test(@id, "^hypProduct_[0-9]+$")]
Demo:
演示:
>>> from lxml.html import fromstring
>>>
>>> data = '<a id="hypProduct_10">link1</a>'
>>> tree = fromstring(data)
>>> tree.xpath('//a[re:test(@id, "^hypProduct_[0-9]+$")]', namespaces={'re': "http://exslt.org/regular-expressions"})[0].attrib["id"]
'hypProduct_10'
#1
3
In your case, the expression would be:
在你的情况下,表达式是:
//a[re:test(@id, "^hypProduct_[0-9]+$")]
Demo:
演示:
>>> from lxml.html import fromstring
>>>
>>> data = '<a id="hypProduct_10">link1</a>'
>>> tree = fromstring(data)
>>> tree.xpath('//a[re:test(@id, "^hypProduct_[0-9]+$")]', namespaces={'re': "http://exslt.org/regular-expressions"})[0].attrib["id"]
'hypProduct_10'