1

I am trying this for the first time and am not sure I have quite achieved what i want to. I am pulling in data via a screen scrape as arrays and want to put them into a hash.

I have a model with columns :home_team and :away_team and would like to post the data captured via the screen scrape to these

I was hoping someone could quickly run this in a rb file

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

and advise if this is actually a hash as it seems to be an array as i cant see the hash name being outputted. i would of expected something like this

{"team_clean"=>[{:home_team => "Man Utd", "Chelsea", "Liverpool"}, 
          {:away_team => "Swansea", "Cardiff"}]}

any help appreciated

4

2 に答える 2

1

ここに解決策があります

team_clean = Hash[:team_clean => [Hash[:home_team => home_team,:away_team => away_team]]]
于 2013-03-14T09:32:29.860 に答える