使用Ruby解析HTML文件标题

问题描述 投票:2回答:2

我有一个HTML文件,我想在Ruby中解析。 HTML文件非常简单,只包含标题,链接和段落。我正在使用nokogiri来解析。我正在处理的HTML文件的示例如下:

<h1><a id="Dog_0"></a>Dog</h1>
<h2><a id="Washing_dogs_3"></a>Washing Dogs</h2>
<h3>Use soap</h3>
<h2><a id="Walking_dogs_1"></a>Walking Dogs</h2>

我需要将h1标题视为父母,h2标题为h1标题的子项,h3标题为h2标题的子项,等等...

我想将这些信息存储在一个哈希数组中,这样就可以了

[ { 
   h1: "Dog",
 link: "Dog_0",  
},{
   h1: "Dog",
   h2: "Washing Dogs",
   link: "Dog_0#Washing_dogs_3"
},{
   h1: "Dog",
   h2: "Washing Dogs",
   h3: "Use Soap",
   link: "Dog_0#Washing_dogs_3"
},{
   h1: "Dog",
   h2: "Walking Dogs"
   link: "Dog_0#Walking_dogs_1"
}]

由于没有任何节点嵌套,我认为我不能使用任何有用的方法来查找子节点。到目前为止我所拥有的是:

array_of_records = []; #Store the records in an array
desired_headings = ['h1','h2','h3','h4','p'] # headings used to split html 
into records

Dir.glob('*.html') { |html_file|


  nokogiri_object = File.open(html_file) { |f| Nokogiri::HTML(f, nil, 'UTF- 
8') }

  nokogiri_object.traverse { |node|
   next unless desired_headings.include?(node.name)
   record = {}
   record[node.name.to_sym] = node.text.gsub(/[\r\n]/,'').split.join(" ")
   link = node.css('a')[0]

   record[:link] = link['id'] if !link.nil?

   array_of_records << record
  }

此代码设法捕获我正在解析的标题,并将其内容存储在哈希中

 {heading: "content"} 

但不捕获我需要捕获的父类信息。

任何帮助将不胜感激!

html ruby parsing nokogiri
2个回答
0
投票

traverse是个好主意。你想跟踪最新的h1,h2,h3等:......

@state = {}
records = []
nokogiri_object.traverse { |node|
  next unless desired_headings.include?(node.name)
  @state[node.name] = node.text
  case node.name
    when 'h1'
      records << {
        h1: @state['h1']
      }
    when 'h2'
      records << {
        h1: @state['h1'],
        h2: @state['h2'],
      }

  end
}

0
投票

所以,我提出了一个主要有效的解决方案,除了它不是我想要的那样将我的“记录”存储在我的记录数组中。我的解决方案是

require "rubygems"
require "nokogiri"
require "json"   

array_of_records = [] #Store the records in an array
desired_headings = ['h1','h2','h3','h4','p'] # headings used to split html into 
records

Dir.glob('./source/*.html') { |html_file|

  latest_headings = {}; # hash to store latest data from headings

  nokogiri_object = File.open(html_file) { |f| Nokogiri::HTML(f, nil, 'UTF-8') }
  nokogiri_object.traverse { |node|

    next unless desired_headings.include?(node.name)

    case node.name
    when ("h1".."h4")

      @record = {}
      latest_headings[node.name] = node.text
      latest_headings.each { |key,value|
        @record[key] = value if key <= node.name
      }
      link = node.css('a')[0]
      link = link['id'] if !link.nil?
      @record['link'] = link if !link.nil?
    when "p"
      @record['content'] = node.text
    end

    array_of_records << @record
    puts @record

  } #end loop through nodes
 puts array_of_records    

} #end loop through files

我希望puts @record会打印与puts array_of_records打印相同的东西,但我发现array_of_records不包含什么puts @record prints。有什么建议?

© www.soinside.com 2019 - 2024. All rights reserved.