0

したがって、複数の行と列を持つテーブルがあります。

<table>
  <tr>
    <th>Employee Name</th>
    <th>Reg Hours</th>
    <th>OT Hours</th>
  </tr>
  <tr>
    <td>Employee 1</td>
    <td>10</td>
    <td>20</td>
  </tr>
  <tr>
    <td>Employee 2</td>
    <td>5</td>
    <td>10</td>
  </tr>
</table>

別の表もあります。

<table>
  <tr>
    <th>Employee Name</th>
    <th>Revenue</th>
  </tr>
    <td>Employee 2</td>
    <td>$10</td>
  </tr>
  <tr>
    <td>Employee 1</td>
    <td>$50</td>
  </tr>
</table>

従業員の順序はテーブル間でランダムである可能性があることに注意してください。

nokogiriを使用して、各従業員をオブジェクトとして、合計時間と収益を含むjsonファイルを作成するにはどうすればよいですか?

現在、いくつかのxpathを使用して個々のテーブルセルを取得することができます。例えば:

puts page.xpath(".//*[@id='UC255_tblSummary']/tbody/tr[2]/td[1]/text()").inner_text

編集:

ページオブジェクトgemと@Dave_McNullaからのリンクを使用して、このコードを試して、何が得られるかを確認しました。

class MyPage
  include PageObject

  table(:report, :id => 'UC255_tblSummary')

  def get_some_information
    report_element[1][2].text
  end
end

puts get_some_information

ただし、何も返されていません。

データ:https ://gist.github.com/anonymous/d8cc0524160d7d03d37b

時間表の複製があります。最初のものは大丈夫です。必要なもう1つのテーブルは、アクセサリの収益テーブルです。(アクティベーションテーブルも必要ですが、時間とアクセサリの収益テーブルをマージするコードからそれをマージしようとします。

4

1 に答える 1

5

一般的なアプローチは次のとおりです。

  1. キーが従業員であるテーブルごとにハッシュを作成します
  2. 両方のテーブルの結果をマージします
  3. JSONに変換

キーが従業員であるテーブルごとにハッシュを作成します

この部分は、ワティルまたはノコギリで行うことができます。大きなテーブルが原因でWatirのパフォーマンスが低下している場合にのみ、Nokogiriを使用するのが理にかなっています。

ワティル:

#I assume you would have a better way to identify the tables than by index
hours_table = browser.table(:index, 0)
wage_table = browser.table(:index, 1)

#Turn the tables into a hash
employee_hours = {}
hours_table.trs.drop(1).each do |tr| 
    tds = tr.tds
    employee_hours[ tds[0].text ] = {"Reg Hours" => tds[1].text, "OT Hours" => tds[2].text}     
end
#=> {"Employee 1"=>{"Reg Hours"=>"10", "OT Hours"=>"20"}, "Employee 2"=>{"Reg Hours"=>"5", "OT Hours"=>"10"}}

employee_wage = {}
wage_table.trs.drop(1).each do |tr| 
    tds = tr.tds
    employee_wage[ tds[0].text ] = {"Revenue" => tds[1].text}   
end
#=> {"Employee 2"=>{"Revenue"=>"$10"}, "Employee 1"=>{"Revenue"=>"$50"}}

のこぎり:

page = Nokogiri::HTML.parse(browser.html)

hours_table = page.search('table')[0]
wage_table = page.search('table')[1]

employee_hours = {}
hours_table.search('tr').drop(1).each do |tr| 
    tds = tr.search('td')
    employee_hours[ tds[0].text ] = {"Reg Hours" => tds[1].text, "OT Hours" => tds[2].text}     
end
#=> {"Employee 1"=>{"Reg Hours"=>"10", "OT Hours"=>"20"}, "Employee 2"=>{"Reg Hours"=>"5", "OT Hours"=>"10"}}

employee_wage = {}
wage_table.search('tr').drop(1).each do |tr| 
    tds = tr.search('td')
    employee_wage[ tds[0].text ] = {"Revenue" => tds[1].text}   
end
#=> {"Employee 2"=>{"Revenue"=>"$10"}, "Employee 1"=>{"Revenue"=>"$50"}}

両方のテーブルの結果をマージします

2つのハッシュをマージして、特定の従業員のハッシュに時間と収益が含まれるようにします。

employee = employee_hours.merge(employee_wage){ |key, old, new| new.merge(old) }
#=> {"Employee 1"=>{"Revenue"=>"$50", "Reg Hours"=>"10", "OT Hours"=>"20"}, "Employee 2"=>{"Revenue"=>"$10", "Reg Hours"=>"5", "OT Hours"=>"10"}}

JSONに変換

この前の質問に基づいて、ハッシュをjsonに変換できます。

require 'json'
employee.to_json
于 2013-03-20T16:49:57.577 に答える