Created
January 30, 2015 20:16
-
-
Save huezoaa/0cd35c3cd5edac9c581b to your computer and use it in GitHub Desktop.
Stock Ticker
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
require 'HTTParty' | |
require 'json' | |
require 'nokogiri' | |
### run a GET from Yahoo with HTTParty for Apple Stock, assign to response | |
response = HTTParty.get('http://finance.yahoo.com/q?s=AAPL') | |
### take response body and use Nokogiri to parse the HTML into a Ruby variable | |
ticker = Nokogiri::HTML(response.body) | |
### use the xpath method to isolate the stock value from the parsed HTML | |
### use the .first method to get the first element of the NodeSet | |
puts ticker.xpath("//span[@id='yfs_l84_aapl']").first | |
### The output of the above "contains" my desired value. | |
### Use the .content method to get the value only: | |
puts ticker.xpath("//span[@id='yfs_l84_aapl']").first.content | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment