Grabbing values from Hash ruby -
bit lost on one. grabbing table data http://www.bbc.co.uk/sport/football/tables using nokogiri. sort unsure how grab of data wanted, kindly provided example of looks so
require 'open-uri' require 'nokogiri' url = 'http://www.bbc.co.uk/sport/football/tables' doc = nokogiri::html.parse(open url) teams = doc.search('tbody tr.team') keys = teams.first.search('td').map |k| k['class'].gsub('-', '_').to_sym end hsh = teams.flat_map |team| hash[keys.zip(team.search('td').map(&:text))] end puts hsh
im little unclear of this, can see final output hsh gives hash example
{:statistics=>"", :position=>"no movement 20", :team_name=>"reading", :played=>"33", :home_won=>"4", :home_drawn=>"7", :home_lost=>"6", :home_for=>"23", :home_against=>"31", :away_won=>"1", :away_drawn=>"2", :away_lost=>"13", :away_for=>"13", :away_against=>"32", :goal_difference=>"-27", :points=>"24", :last_10_games=>"\n draw\nwin\nloss\nloss\nloss\nloss\nloss\nloss\nloss\ndraw", :status=>"\n report of reading's last match "} {:statistics=>"", :position=>"no movement 19", :team_name=>"qpr", :played=>"33", :home_won=>"2", :home_drawn=>"8", :home_lost=>"6", :home_for=>"12", :home_against=>"23", :away_won=>"2", :away_drawn=>"4", :away_lost=>"11", :away_for=>"17", :away_against=>"31", :goal_difference=>"-25", :points=>"24", :last_10_games=>"\n draw\ndraw\nloss\nloss\nwin\nwin\nloss\nloss\ndraw\nloss", :status=>"\n report of qpr's last match "}
so thinking grab each :team_name go through loop would
hsh.each |k,v] v['team_name'] end
so give me value of team_name every hash? im trying achieve @ extracting elements nokogiri make sense of provided solution , save each value model
could clarify understanding please
thanks
this doesn't useful, iterates on hsh
doesn't because value want in k
:
hsh.each |k,v] v['team_name'] end
the problem thathsh
array of hashes. keeping same name, hsh
, treating array:
hsh.map{ |a| a[:team_name]}
returns:
[ [ 0] "man utd", [ 1] "man city", [ 2] "chelsea", [ 3] "arsenal", [ 4] "tottenham", [ 5] "everton", [ 6] "liverpool", [ 7] "west brom", [ 8] "swansea", [ 9] "fulham", [10] "west ham", [11] "southampton", [12] "newcastle", [13] "norwich", [14] "sunderland", [15] "stoke", [16] "aston villa", [17] "wigan", [18] "qpr", [19] "reading" ]
it might bit easier visualize if @ first 2 entries in hsh
using hsh[0,2]
:
[15] (pry) main: 0> hsh[0,2] [ [0] { :statistics => "", :position => "no movement 1", :team_name => "man utd", :played => "33", :home_won => "14", :home_drawn => "0", :home_lost => "2", :home_for => "40", :home_against => "17", :away_won => "12", :away_drawn => "3", :away_lost => "2", :away_for => "35", :away_against => "18", :goal_difference => "40", :points => "81", :last_10_games => "\n win\nwin\nwin\nwin\nwin\nwin\nwin\nloss\nwin\ndraw", :status => "\n report of man utd's last match " }, [1] { :statistics => "", :position => "no movement 2", :team_name => "man city", :played => "32", :home_won => "12", :home_drawn => "3", :home_lost => "1", :home_for => "36", :home_against => "11", :away_won => "8", :away_drawn => "5", :away_lost => "3", :away_for => "22", :away_against => "16", :goal_difference => "31", :points => "68", :last_10_games => "\n win\ndraw\ndraw\nloss\nwin\nwin\nloss\nwin\nwin\nwin", :status => "\n report of man city's last match " } ]
i dont understand how got returned hash
you got array of hashes because:
hash[keys.zip(team.search('td').map(&:text))]
coerces array of key/value pairs hash. got array of key/value pairs because keys.zip(team.search('td').map(&:text))
interweaves keys
, results of team.search('td').map(&:text)
.
so save each team name model, can iterate through loop?
file.open('team_names.txt', 'w') |fo| fo.puts hsh.map{ |a| a[:team_name] } end
it might hsh.map{ |a| a[:team_name] }.map(&:to_s)
needed, puts
to_s
on every element in returned array automatically, auto-converting symbols strings you.
if want map out team_name , position, have create seperate calls, ie map on each value? cant map out multiple values in same call can i?
this create array of arrays:
ary_of_arys = hsh.map{ |a| [ a[:team_name], a[:position] ] }
this join inner arrays each row, print out rows:
file.open('team_names.txt', 'w') |fo| fo.puts hsh.map{ |a| [ a[:team_name], a[:position] ].join(', ') } end
Comments
Post a Comment