Lxml 树在给定时以字符串形式返回 xpath 表达式,如以下示例所示


import lxml.etree as LX
url= "https://www.w3.org/2003/05/soap-envelope/"
response = requests.get(url)
xml_string = LX.fromstring(response.content)
path='"//*[1.1.1]"'
result = xml_string.xpath(path)
print(result)

上述代码的结果将 xpath 作为字符串本身返回,并且不会通过无效 xpath 的错误

'"//*[1.1.1]"'不是 XPath(*(。这是一个字符串。请注意报价位置。

只包含一个字符串的 XPath 表达式将返回该字符串,这并不奇怪。

import lxml.etree as LX
import requests
url = "https://www.w3.org/2003/05/soap-envelope/"
response = requests.get(url)
tree = LX.fromstring(response.content)
path = '"Look at me I'm a string!"'
result = tree.xpath(path)
print(result)

指纹

看我,我是一根绳子!

(*(吹毛求疵技术点。当然,这是一个 XPath 表达式。 如果不是,您将从 lxml 收到错误。它只是不是一个选择任何元素的人。

最新更新