0

嗨,我是 nokogiri 的新手,正在尝试解析具有不同树结构的 HTML 文档。关于如何解析它的任何建议都会很棒。我想捕获此页面上的所有文本。

<div class = "main"> Title</div>
<div class = "subTopic">
    <span = "highlight">Sub Topic</span>Stuff
</div>

<div class = "main"> Another Title</div>
<div class = "subTopic">
    <span class = "highlight">Sub Topic Title I</span>Stuff<br>
    <span class = "highlight">Sub Topic Title II</span>Stuff<br>
    <span class = "highlight">Sub Topic Title III</span>Stuff<br>
</div>  

我试过这个,但它只是把每个完整的数组都放出来,我什至不知道如何进入“Stuff”部分。

content = Nokogiri::HTML(open(@url))
content.css('div.main').each do |m|
    puts m .text
    content.css('div.subTopic').each do |s|
        puts s.text
        content.css('span.highlight').each do |h|
            puts h.text
        end
    end
end         

帮助将不胜感激。

4

1 回答 1

0

类似的东西会解析你的给定文档结构:

数据

<div class="main"> Title</div>
<div class="subTopic">
    <span class="highlight">Sub Topic</span>Stuff
</div>

<div class = "main"> Another Title</div>
<div class = "subTopic">
    <span class = "highlight">Sub Topic Title I</span>Stuff<br>
    <span class = "highlight">Sub Topic Title II</span>Stuff<br>
    <span class = "highlight">Sub Topic Title III</span>Stuff<br>
</div>

代码:

require 'nokogiri'
require 'pp'

content = Nokogiri::HTML(File.read('text.txt'));

topics = content.css('div.main').map do |m|
    topic={}
    topic['title'] = m.text.strip
    topic['highlights'] = m.xpath('following-sibling::div[@class=\'subTopic\'][1]').css('span.highlight').map do |h|
      topic_highlight = {}
      topic_highlight['highlight'] = h.text.strip
      topic_highlight['text'] = h.xpath('following-sibling::text()[1]').text.strip
      topic_highlight
    end
    topic
end

pp topics

将打印:

[{"title"=>"Title",
  "highlights"=>[{"highlight"=>"Sub Topic", "text"=>"Stuff"}]},
 {"title"=>"Another Title",
  "highlights"=>
   [{"highlight"=>"Sub Topic Title I", "text"=>"Stuff"},
    {"highlight"=>"Sub Topic Title II", "text"=>"Stuff"},
    {"highlight"=>"Sub Topic Title III", "text"=>"Stuff"}]}]
于 2013-03-14T05:01:49.083 回答