Python LXML XPATH Returns 有效查询错误
Python LXML XPATH Returns Error With Valid Query
我是运行以下查询:
//div[@class="review-list"]//div[@class="review review--with-sidebar"]//div[@class="review-content"]/p/string(.)
我收到以下错误:
lxml.etree.XPathEvalError: Invalid expression
但是,如果我使用以下符号:
//div[@class="review-list"]//div[@class="review review--with-sidebar"]//div[@class="review-content"]/p/text()
一切顺利。
我假设问题出在 string(.)
表示法的使用上,但是当我测试它时 here 它没有问题,所以我假设它的语法有效。
我基本上是运行下面的代码:
from lxml import html
tree = html.fromstring(PAGE_CONTENT)
results = tree.xpath(QUERY)
是否有我可以使用的替代方法允许使用这样的表达式? string-join
似乎会导致类似的问题。
你的错误原因是符号
.../string(.)
仅在 XPath 2.0 或更高版本中有效。在 XPath 1.0 中它是无效的并抛出错误。
在 XPath-1.0 中有效的替代方法是将整个表达式包装在 string(...)
函数中,如下所示:
string(//div[@class="review-list"]//div[@class="review review--with-sidebar"]//div[@class="review-content"]/p)
我是运行以下查询:
//div[@class="review-list"]//div[@class="review review--with-sidebar"]//div[@class="review-content"]/p/string(.)
我收到以下错误:
lxml.etree.XPathEvalError: Invalid expression
但是,如果我使用以下符号:
//div[@class="review-list"]//div[@class="review review--with-sidebar"]//div[@class="review-content"]/p/text()
一切顺利。
我假设问题出在 string(.)
表示法的使用上,但是当我测试它时 here 它没有问题,所以我假设它的语法有效。
我基本上是运行下面的代码:
from lxml import html
tree = html.fromstring(PAGE_CONTENT)
results = tree.xpath(QUERY)
是否有我可以使用的替代方法允许使用这样的表达式? string-join
似乎会导致类似的问题。
你的错误原因是符号
.../string(.)
仅在 XPath 2.0 或更高版本中有效。在 XPath 1.0 中它是无效的并抛出错误。
在 XPath-1.0 中有效的替代方法是将整个表达式包装在 string(...)
函数中,如下所示:
string(//div[@class="review-list"]//div[@class="review review--with-sidebar"]//div[@class="review-content"]/p)