2011-09-21 36 views
0

我已經搜索並搜索了3天,現在試圖讓數據刮取器工作,並且好像我成功解析了看起來像的HTML表格這個:如何在使用ruby/nokogiri解析html表格後得到正確的值

<tr class='ds'> 
<td class='ds'>Length:</td> 
<td class='ds'>1/8"</td> 
</tr> 
<tr class='ds'> 
<td class='ds'>Width:</td> 
<td class='ds'>3/4"</td> 
</tr> 
<tr class='ds'> 
<td class='ds'>Color:</td> 
<td class='ds'>Red</td> 
</tr> 

但是,我似乎無法得到它打印到csv正確。

的Ruby代碼如下:

Specifications = { 
:length => ['Length:','length','Length'],  
:width => ['width:','width','Width','Width:'], 
:Color => ['Color:','color'], 
....... 
}.freeze 

def specifications 
    @specifications ||= xml.css('tr.ds').map{|row| row.css('td.ds').map{|cell| cell.children.to_s } }.map{|record| 
    specification = Specifications.detect{|key, value| value.include? record.first } 
    [specification.to_s.titleize, record.last] } 
end 

以及CSV被打印成一列(似乎是全陣列):

[["", nil], ["[:finishtype, [\"finish\", \"finish type:\", \"finish type\", \"finish type\", \"finish type:\"]]", "Metal"], ["", "1/4\""], ["[:length, [\"length:\", \"length\", \"length\"]]", "18\""], ["[:width, [\"width:\", \"width\", \"width\", \"width:\"]]", "1/2\""], ["[:styletype, [\"style:\", \"style\", \"style:\", \"style\"]]"........ 

我相信這個問題是我沒有指定要返回的值,但是當我嘗試指定輸出時,我沒有成功。任何幫助將不勝感激!

+1

什麼是你期望的輸出:

如果你遇到屬性不匹配您Specification,你可以只通過改變下降值是多少? –

回答

0

嘗試改變

[specification.to_s.titleize, record.last] 

[specification.last.first.titleize, record.last] 

detect產生例如[:length, ["Length:", "length", "Length"]]這將變成 "[:length, [\"Length:\", \"length\", \"Length\"]]"to_s。用last.first你可以只提取它的部分"Length:"

xml.css('tr.ds').map{|row| row.css('td.ds').map{|cell| cell.children.to_s } }.map{|record| 
     specification = Specifications.detect{|key, value| value.include? record.first } 
     [specification.last.first.titleize, record.last] if specification 
    }.compact 
+0

感謝您的回覆。我嘗試了這個調整,它吐了我:耙子中止了! 未定義的方法'last'爲零:NilClass – Sky

+0

如果您的HTML中的數據不符合'Specifications「(當'detect'沒有找到關鍵字時,您得到了'nil'),就會發生這種情況。你以前的代碼會愉快地將'nil'轉換成一個字符串,並且你得到''[「」,nil]「'。問題是你想在這種情況下看到什麼。 –

+0

我不知道這是否有幫助,但我有這在紅寶石文本(爲csv)的結尾:#CSV標題字段 #Format =:field_name => defaultvalue F = {:item_name => nil,:長度=>零,:寬度=>無,:類型=>零,...} .freeze – Sky