使用Nokogiri从交替行收集数据

时间:2013-04-12 14:03:40

标签: ruby nokogiri mechanize

我有这个样本表数据......

<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文件中的一行的任何想法?并从整个表中获取所有数据?

1 个答案:

答案 0 :(得分:3)

您可以获取该表的所有'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"]