How to split a HTML document using Nokogiri?

徘徊边缘 提交于 2019-12-07 17:20:32

问题


Right now, I'm splitting the HTML document to small pieces like this: (regular expression simplified - skipping header tag content and closing tag)

document.at('body').inner_html.split(/<\s*h[2-6][^>]*>/i).collect do |fragment|
  Nokogiri::HTML(fragment)
end

Is there an easier way to perform that splitting?

The document is very simple, just headers, paragraphs and formatted text in it. For example:

<body>
<h1>Main</h1>
<h2>Sub 1</h2>
<p>Text</p>
-----
<h2>Sub 2</h2>
<p>Text</p>
-----
<h3>Sub 2.1</h3>
<p>Text</p>
-----
<h3>Sub 2.2</h3>
<p>Text</p>
</body>

For that sample, I need to get four pieces.


回答1:


I just had to do something similar. I split a large HTML file in to "chapters" where a chapter is started by an <h1> tag.

I also wanted to keep the title of the chapters in the hash and ignore everything before the first <h1> tag.

Here is the code:

full_book = Nokogiri::HTML(File.read('full-book.html'))
@chapters = full_book.xpath('//body').children.inject([]) do |chapters_hash, child|
  if child.name == 'h1'
    title = child.inner_text
    chapters_hash << { :title => title, :contents => ''}
  end

  next chapters_hash if chapters_hash.empty?
  chapters_hash.last[:contents] << child.to_xhtml
  chapters_hash
end


来源:https://stackoverflow.com/questions/3484874/how-to-split-a-html-document-using-nokogiri

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!