Created
April 19, 2013 17:49
-
-
Save ddrscott/5421953 to your computer and use it in GitHub Desktop.
scrape xe.com using Ruby and Nokogiri
This file contains 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
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