Skip to content

Instantly share code, notes, and snippets.

@ddrscott
Created April 19, 2013 17:49
Show Gist options
  • Save ddrscott/5421953 to your computer and use it in GitHub Desktop.
Save ddrscott/5421953 to your computer and use it in GitHub Desktop.
scrape xe.com using Ruby and Nokogiri
api_url = "http://www.xe.com/currencytables/?from=USD&date=#{Time.now.strftime('%Y-%m-%d')}"
doc = Nokogiri(open(api_url))
rows = doc.search('#historicalRateTbl tr')
rows.shift # disgard header
currencies = {}
while row = rows.shift
currency = row.children[0].inner_text
rate_to_us = row.children[2].inner_text
# rate_from_us = row.children[3].inner_text
currencies[currency] = rate_to_us.to_f
end
# => {"AED"=>3.67308,
# "AFN"=>53.5272,
# "ALL"=>107.263,
# "AMD"=>420.689,
# "ANG"=>1.7888,
# "AOA"=>96.0106,
# "ARS"=>5.15805,
# "AUD"=>0.970905,
# "AWG"=>1.7899,
# "AZN"=>0.7847,
# "BAM"=>1.49669,
# ...
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment