我是第一次尝试这个,我不确定我是否已经完全实现了我想要的。我通过屏幕抓取将数据作为数组提取,并希望将它们放入哈希中。
我有一个带有 :home_team 和 :away_team 列的模型,并且想将通过屏幕抓取捕获的数据发布到这些
我希望有人可以在 rb 文件中快速运行它
require 'open-uri'
require 'nokogiri'
FIXTURE_URL = "http://www.bbc.co.uk/sport/football/premier-league/fixtures"
doc = Nokogiri::HTML(open(FIXTURE_URL))
home_team = doc.css(".team-home.teams").map {|team| team.text.strip}
away_team = doc.css(".team-away.teams").map {|team| team.text.strip}
team_clean = Hash[:home_team => home_team, :away_team => away_team]
puts team_clean.inspect
并建议这是否实际上是一个哈希,因为它似乎是一个数组,因为我看不到正在输出的哈希名称。我会期待这样的事情
{"team_clean"=>[{:home_team => "Man Utd", "Chelsea", "Liverpool"},
{:away_team => "Swansea", "Cardiff"}]}
任何帮助表示赞赏