我有这个示例表数据…
<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
关于如何在我的CSV文件中将这两个<tr>
放入一行,有什么想法吗?然后从整个表中获取所有数据?
您可以获取表的所有'trs',然后使用each_slice(2)
获取相关行对。
html = %q{
<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>
<tr class="main">
<td align="left">2/4/05</td>
<td>223-334</td>
<td></td>
<td></td>
<td></td>
<td align="right">$4.00</td>
</tr>
<tr style="background-color:#FFFFD7">
<td colspan="2">Company Name2</td>
<td colspan="4">Owner Name2</td>
</tr>
</table>}
data_table = Nokogiri::HTML(html)
table = data_table.xpath('//table[@class="main"]')
rows = table.xpath('tr')
rows.each_slice(2) do |row_pair|
date = row_pair[0].at_xpath('./td[1]').text
id = row_pair[0].at_xpath('./td[2]').text
amount = row_pair[0].at_xpath('./td[6]').text
company = row_pair[1].at_xpath('./td[1]').text
name = row_pair[1].at_xpath('./td[2]').text
p [date, id, amount, company, name]
end
给出您想要的数组(并且可以放在您的csv中):
["3/4/05", "123-334", "$2.00", "Company Name", "Owner Name"]
["2/4/05", "223-334", "$4.00", "Company Name2", "Owner Name2"]