0

コミュニティの助けを借りて、いくつかの gif と URL を HTML テーブルの有用なデータに置き換えてから、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 に低下した場合)。考える?配列を比較することはできますが、レコードの取得が高速であるため、変更があるたびに 2 次元配列を保存するのではなく、個別の変更のみを保存できると思います (これはすぐに制御不能になると思います)。

...おそらくご想像のとおり、私もこれを頭の中で理解しようとしています;)スコットに感謝します

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 に答える