- Find the div you want.
- Find the stop item you want, and then find all the following siblings.
- Remove them.
例如:
<body>
<div id="a">
<h2>My Section</h2>
<p class="backtotop">Back to Top</p>
<p>More Content</p>
<p>Even More Content</p>
</div>
</body>
require nokogiri
doc = Nokogiri::HTML(my_html)
div = doc.at( #a )
div.at( .backtotop ).xpath( following-sibling::* ).remove
puts div
#=> <div id="a">
#=> <h2>My Section</h2>
#=> <p class="backtotop">Back to Top</p>
#=>
#=>
#=> </div>
更复杂的例子是<代码>后至 2. 本项不得成为四分五裂的根源:
<body>
<div id="b">
<h2>Another Section</h2>
<section>
<p class="backtotop">Back to Top</p>
<p>More Content</p>
</section>
<p>Even More Content</p>
</div>
</body>
require nokogiri
doc = Nokogiri::HTML(my_html)
div = doc.at( #b )
n = div.at( .backtotop )
until n==div
n.xpath( following-sibling::* ).remove
n = n.parent
end
puts div
#=> <div id="b">
#=> <h2>Another Section</h2>
#=> <section><p class="backtotop">Back to Top</p>
#=>
#=> </section>
#=> </div>
如果您的超文本比上述内容更加复杂,请提供您希望得到的实际样本。 这是你今后提出的任何问题的良好建议。