我有这个示例表数据...
<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
关于如何将这两个<tr>
' 放在我的 CSV 文件中的一行的任何想法?并从整个表中获取所有数据?