Created
October 8, 2013 09:13
-
-
Save timrandg/6881919 to your computer and use it in GitHub Desktop.
web parser challenge for Ben
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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