我已經搜索並搜索了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\"]]"........
我相信這個問題是我沒有指定要返回的值,但是當我嘗試指定輸出時,我沒有成功。任何幫助將不勝感激!
什麼是你期望的輸出:
如果你遇到屬性不匹配您
Specification
,你可以只通過改變下降值是多少? –