Skip to content

Instantly share code, notes, and snippets.

@huezoaa
Created January 30, 2015 20:16
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 huezoaa/0cd35c3cd5edac9c581b to your computer and use it in GitHub Desktop.
Save huezoaa/0cd35c3cd5edac9c581b to your computer and use it in GitHub Desktop.
Stock Ticker
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