我已经从社区获得了一些帮助,在将它放入2D数组之前将一些gif和url替换为HTML表中的有用数据,但我认为我实际需要的是将表的每一行存储为在activerecord条目中哈希。
以下是带标题的第一行示例数据:
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&dylan=0&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&dylan=0&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°C</td>
<td class="status" style="background-color: #fefe00"> 
</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代码 - =但我正在创建的哈希值并不像我希望的那样有用 - 所以我宁愿使用哈希创建一个哈希值表头作为键 然后我可以将服务器序列号和aray地址输入到activerecord条目中,以便我可以比较和显示记录实例之间的增量(例如,如果驱动器运行状况从10减少到5) 你们都觉得怎么样? 我可以比较数组,但我认为由于记录检索速度很快,我只能存储不同的更改,而不是每次存在更改时存储二维数组(我认为这会很快失控)
......正如你可能猜到的那样,我也试图把这一点直接放在脑海里;) 非常感谢 斯科特
答案 0 :(得分: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