我保留了我的代码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)
答案 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