0

在将 HTML 表中的一些 gif 和 url 替换为有用的数据之前,我得到了社区的一些帮助,然后将其放入 2D 数组中,但我认为我真正需要的是将表的每一行作为哈希存储在活动记录条目。

这是带有标题的第一行示例数据:

html2 = <<TABLE2
<table class="status">
<caption class="status">Drive status</caption>
<tr class="status">
<th class="status"></th>
<th class="status">Drive</th>
<th class="status">State</th>
<th class="status">Health</th>
<th class="status">Make/Model</th>
<th class="status">Speed</th>
<th class="status">Serial</th>
<th class="status">Firmware</th>
<th class="status"><a href="/cgi-bin/status_dylan?cont=0&amp;dylan=0&amp;display=1">Sectors</a></th>
<th class="status">Temp</th>
<th class="status"> </th>
</tr>
<tr class="status">
<td class="status"><img border="0" src="/tick_green.gif"></td>
<td class="status">0</td>
<td class="status">Ready</td>
<td class="status"><a href="/cgi-bin/status_drive?cont=0&amp;dylan=0&amp;drive=0"><img border="0" src="/bar10.gif"></a></td>
<td class="status">SEAGATE ST3146807FC</td>
<td class="status">10000 RPM</td>
<td class="status">3HY61E1B</td>
<td class="status">XR12</td>
<td class="status">286749488</td>
<td class="status"> 29.0&#176;C</td>
<td class="status" style="background-color: #fefe00">&#160;
</td>
</tr>

clean_table2 = []
  table2.css('tr').each do |tr|
    clean_row = []
    tr.css('td').each do |td|
      #for each cell, look for img tags, and replace the images with text as appropriate, then strip the html
      img = td.at('img')
      clean_row.push case
      when img && img[:src][/bar(\d+)\.gif/] then 'Health: '+$1
      when img && img[:src][/tick_green/] then 'Healthy'
      when img && img[:src][/cross_red/] then 'Failed'
      when img && img[:src][/caution/] then 'Caution'
      else td.text.strip
      end

    end
  clean_table2.push clean_row
  #puts clean_row[5]
  end
  puts "\n"
#puts clean_table.join("\n")
clean_table2.each {|x|
  puts "#{x}"
}

这是删除所有不重要的内容并用合理文本替换“无用”gif的代码-=但是我创建的散列并不像我希望的那样有用-所以我宁愿使用表头创建一个散列作为键然后我可以将它与服务器序列号和数组地址一起输入到一个活动记录条目中,以便我可以比较和显示记录实例之间的增量(例如,如果驱动器运行状况从 10 下降到 5)你们都怎么做思考?我可以比较数组,但我认为由于记录检索速度很快,我只能存储不同的变化,而不是每次发生变化时都存储一个二维数组(我认为这会很快失控)

...正如您可能猜到的那样,我也试图在我的脑海中直截了当;)非常感谢斯科特

4

1 回答 1

0

稍微改写一下,让它更合乎逻辑......

  table = html_page.parser.xpath('//table/caption[contains(.,"Drive")]/..')
  #loop through each row individually (or do I want to chuck the whole thing into a nice juicy hash)
  #Am I using this?  #REMOVE
  clean_table = Array.new
  clean_head=[]
  table.css('tr').each do |tr|
    #stash WWN number, fake interface and fake address [can get, but not needed at this stage]
    clean_row = {:wwn=>cells[0],:dyl_if=>'1',:dyl_addr=>'0'}
    #grab headers
    tr.css('th').each_with_index do |th,i| 
      if i == 0
        clean_head.push "Drive Health"
      else if i == 10
        clean_head.push "BG Temp"
      else clean_head.push th.text.strip
      end
    end
  end
    #each td in each tr - add index so I can add table headers as keys in hash
    tr.css('td').each_with_index do |td, i|
      #for each cell, look for img tags, and replace the images with text as appropriate, then strip the html
      img = td.at('img')

      clean_row[clean_head[i]] = case 
      when img && img[:src][/bar(\d+)\.gif/] then 'Health: '+$1
      when img && img[:src][/tick_green/] then 'Healthy'
      when img && img[:src][/cross_red/] then 'Failed'
      when img && img[:src][/caution/] then 'Caution'
    else td.text.strip
      end
    end
    #Debug output - confirm nothing cocked up
    puts clean_row
    if clean_row.has_key?("Health")
      Drive_Record.create(clean_row)
      puts "Add Drive Recprd"
     end

end
于 2012-04-13T03:09:04.497 回答