Nokogiri用于在唯一标签集之间选择文本和html

时间:2012-09-18 13:43:13

标签: ruby xpath css-selectors nokogiri

我正在尝试使用Nokogiri从两个独特的标签集之间提取文本。

<h2 class="point">The problem</h2><h2 class="point">The solution</h2>之间的p-tag中获取文本的最佳方法是什么,然后是<h2 class="point">The solution</h2><div class="frame box sketh">之间的所有HTML?

完整html的示例:

<h2 class="point">The problem</h2>
<p>TEXT I WANT </p>
<h2 class="point">The solution</h2>
HTML I WANT with it's own set of tags (but never an <h2> or <div>)
<div class="frame box sketh"><img src="URL for Image I want later" alt="" /></div>

谢谢!

2 个答案:

答案 0 :(得分:2)

require 'nokogiri'

doc = Nokogiri.HTML(DATA)
doc.search('//h2/following-sibling::node()[name() != "h2" and name() != "div" and text() != "\n"]').each do |block|
  p block.text
end

__END__
<h2 class="point">The problem</h2>
<p>TEXT I WANT</p>
<h2 class="point">The solution</h2>
<div>dont capture this</div>
<span>HTML I WANT with it's <p>own set <b>of</b> tags</p></span>
<div class="frame box sketh"><img src="URL for Image I want later" alt="" /></div>

输出:

"TEXT I WANT"
"HTML I WANT with it's own set of tags"

此XPath选择h2的所有以下兄弟节点,这些节点不是h2div或只包含字符串"\n"

答案 1 :(得分:1)

以下是如何在包含类的两个h2之间获取p标签文本

//h2[@class="point"][1]/following-sibling::p[./following-sibling::h2[@class="point"]]/text()

对于第二,您应该探索w3schools,并以第一个为例进行操作。