将表行存储为对象(?)使用Nokogiri

时间:2014-12-13 09:45:24

标签: ruby-on-rails ruby web-scraping nokogiri

我保留了我的代码D.R.Y.并希望保存下面的第一个实例变量(@most_recent_game_tr),以便稍后我可以将其称为SomeObject并获取其td:nth-child(x).text。您可以使用哪种(不一定)Nokogiri方法来实现这一目标?

@most_recent_game_tr = doc.css("#games tbody tr:nth-child(#{game_played})")

@result = doc.css("#games tbody tr:nth-child(#{game_played}) td:nth-child(6)").text
@opponent_name = doc.css("#games tbody tr:nth-child(#{game_played}) td:nth-child(5)").text
@rangers = doc.css("#games tbody tr:nth-child(#{game_played}) td:nth-child(8)").text
@extra_time = doc.css("#games tbody tr:nth-child(#{game_played}) td:nth-child(7)").text
@opponent = doc.css("#games tbody tr:nth-child(#{game_played}) td:nth-child(9)").text
@date_result = Date.parse(doc.css("#games tbody tr:nth-child(#{game_played}) td:nth-child(2)").text)
@date = Date.parse(doc.css("#games tbody tr:nth-child(#{game_played + 1}) td:nth-child(2)").text)

1 个答案:

答案 0 :(得分:0)

能够像这样解决

@most_recent_game_tr = doc.css("#games tbody tr:nth-child(#{game_played})")

@result = @most_recent_game_tr.at_css("td:nth-child(6)").text