我有一个 HTML 结构,如:
<div class='content'>
<h2>Title</h2>
<p>Some content for Title</p>
<h2>Another Title</h2>
<p>Content for Another Title</p>
<p>Some more content for Another title</p>
<h2>Third</h2>
<p>Third Content</p>
</div>
我正在尝试编写代码以输出:
Title
- Some content for Title
Another Title
- Content for Another Title
- Some more content for Another title
Third
- Third Content
直到五分钟前我才使用 Nokogiri,到目前为止我能想到的只有:
content = doc.at_css('.content')
content.css('h2').each do |node|
puts node.text
end
content.css('p').each do |node|
puts " - "
puts node.text
end
这显然不会将各个部分组合在一起。如何使用 Nokogiri 实现所需的分组?