希望您正在寻找以下内容:
require 'nokogiri'
doc = Nokogiri::HTML::Document.parse <<-_HTML_
<p>
This is just an example, how to remove the next sentence using nokogiri in Ruby.
Thank you for your help.
<strong> XXXX </strong>
<br/>
<br />
I want to remove all the HTML after the strong XXXX
<br />
<br />
<strong> YYY </strong>
</p>
_HTML_
puts doc.at('//p/text()[1]').to_s.strip
# >> This is just an example, how to remove the next sentence using nokogiri in Ruby.
# >> Thank you for your help.
现在,如果您想从源 html 本身中删除不需要的 html 内容,那么您可以尝试以下操作:
require 'nokogiri'
doc = Nokogiri::HTML::Document.parse <<-_HTML_
<p>
This is just an example, how to remove the next sentence using nokogiri in Ruby.
Thank you for your help.
<strong> XXXX </strong>
<br/>
<br />
I want to remove all the HTML after the strong XXXX
<br />
<br />
<strong> YYY </strong>
</p>
_HTML_
doc.xpath('//p/* | //p/text()').count # => 10
ndst = doc.search('//p/* | //p/text()')[1..-1]
ndst.remove
puts doc.to_html
# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html><body><p>
# >> This is just an example, how to remove the next sentence using nokogiri in Ruby.
# >> Thank you for your help.
# >> </p></body></html>