我需要获取文本,但只能在特定文本('---------------')之前获取。
例如。 HTML代码示例:
...
<p> This is correct text. Everything after it is wrong</p>
<p>---------------------</p>
<p><strong>This is wrong text</strong></p>
<p> This is wrong another text</p>
...
我正在尝试使用下一个XPath表达式解决此问题:
/p/text()[normalize-space()][not(ancestor::p[contains(.,'---')])]
但不幸的是,这无法按预期进行。
对于正确的解决方案将不胜感激。
最佳答案
此XPath将选择p
的文本,该文本紧随其后的同级包含---
:
//p[following-sibling::p[contains(.,'---')]][1]/text()
关于html - 获取文本,后跟某些文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33744392/