このサンプルテーブルデータがあります...
<table class="main">
<tr class="main">
<td align="left">3/4/05</td>
<td>123-334</td>
<td></td>
<td></td>
<td></td>
<td align="right">$2.00</td>
</tr>
<tr style="background-color:#FFFFD7">
<td colspan="2">Company Name</td>
<td colspan="4">Owner Name</td>
</tr>
.....
This goes on like this with every other <tr> holding
information that I need together.
......
</table>
私はこのコードを書きましたが、すべてではなく最初のコードを取得しているだけです...
data_table = Nokogiri::HTML(page_body, 'UTF-8')
FasterCSV.open('data.csv', 'a') do |csv|
table = data_table.xpath('//table[@class="main"]')
rows = table.xpath('tr')
rows.collect do |row|
date = row.at_xpath('tr[@class="main"]/td[1]/text()')
id = row.at_xpath('tr[@class="main"]/td[2]/text()')
amount = row.at_xpath('tr[@class="main"]/td[3]/text()')
company = row.at_xpath('tr[@style="background-color:#FFFFD7"]/td[1]/text()')
name = row.at_xpath('tr[@style="background-color:#FFFFD7"]/td[2]/text()')
csv << [date, id, amount, company, name]
end
end
これら 2 つ<tr>
の を CSV ファイルの 1 つの行に入れる方法についてのアイデアはありますか? テーブル全体からすべてのデータを取得しますか?