gpt4 book ai didi

python - 当标签名称包含花括号时,XPath 表达式无效

转载 作者:行者123 更新时间:2023-12-02 07:51:00 25 4
gpt4 key购买 nike

我试图访问的标签名称是 {http://whitehatsec.com/XML-api-Vuln}description 。方便的是,每个标签都带有对白帽网站的可爱引用作为前缀。不幸的是xpathlxml不喜欢它。我目前正在尝试vuln_root[0].xpath('//\{http://whitehatsec.com/XML-api-Vuln\}description')这应该让我到达正确的节点。然而lxml一直说


File "test.py", line 23, in <module>
for s in vuln_root[0].xpath('//\{http://whitehatsec.com/XML-api-Vuln\}description'):
File "lxml.etree.pyx", line 1509, in lxml.etree._Element.xpath (src/lxml/lxml.etree.c:50725)
File "xpath.pxi", line 318, in lxml.etree.XPathElementEvaluator.__call__ (src/lxml/lxml.etree.c:146020)
File "xpath.pxi", line 238, in lxml.etree._XPathEvaluatorBase._handle_result (src/lxml/lxml.etree.c:145028)
File "xpath.pxi", line 224, in lxml.etree._XPathEvaluatorBase._raise_eval_error (src/lxml/lxml.etree.c:144883)
lxml.etree.XPathEvalError: Invalid expression

如何解决 xpath 中这个糟糕的标签命名问题?谢谢

最佳答案

这里最简单的方法是仅映射 namespace 。

el.xpath('//vuln:description',
namespaces={'vuln': 'http://whitehatsec.com/XML-api-Vuln'})

关于python - 当标签名称包含花括号时,XPath 表达式无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24461253/

25 4 0