Skip to content

Instantly share code, notes, and snippets.

@timrandg
Created October 8, 2013 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timrandg/6881919 to your computer and use it in GitHub Desktop.
Save timrandg/6881919 to your computer and use it in GitHub Desktop.
web parser challenge for Ben
require 'nokogiri'
require 'open-uri'
#helper function for printing out table, just need to construct a country struct
def print(c)
puts "%-50s %5s %5s %5s" % [ c[:name], c[:abr_two], c[:abr_three], c[:code] ]
end
url = "http://www.nationsonline.org/oneworld/country_code_list.htm"
nodes = Nokogiri::XML(open(url))
#xml path to country table data nodes
table_data = "//td[@class='border1']"
country_data_nodes = nodes.search(table_data)
country_data_nodes.each_slice(5) do |n|
print country = {
#n[0] is flag div. Don't need.
name: n[1].text.strip,
abr_two: n[2].text.strip,
abr_three: n[3].text.strip,
code: n[4].text.strip
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment