首页 > 解决方案 > 获取文本部分到 xpath 中的特定标签

问题描述

我正在尝试使用 Xpath 获取文本直到第一个 <.hr>(ignore dot) 标记

<div class="entry">
   <p> some text</p>
   <p> some text2</p>
   <p> some text3</p>
   <p> some text4</p>
   <hr>(get text part before this hr tag)
   <p> some text5</p>
   <hr>
   <p> some text6</p>
</div>

试过这个

//hr[1]/ancestor::div[@class="entry"]/text()

和一些类似的变体,但无法获得预期的输出

标签: htmlweb-scrapingxpathweb-crawler

解决方案


沿着这些线的东西会给你节点之前的hr节点集

//div[@class="entry"]/*[not(preceding-sibling::hr | self::hr)]

它将列出那些节点

  • 是类名为“entry”的 div 的子项,
  • 前面没有名为 hr 的节点和
  • 本身不是hr节点

推荐阅读