Skip to content

Instantly share code, notes, and snippets.

@pkbanks
Created October 10, 2017 13:04
Show Gist options
  • Save pkbanks/86f008bb9f96fd7c56c4f0c926db8eb3 to your computer and use it in GitHub Desktop.
Save pkbanks/86f008bb9f96fd7c56c4f0c926db8eb3 to your computer and use it in GitHub Desktop.
stock price scraper from google finance, keyed off stock symbol
require 'httparty'
require 'nokogiri'
tickers = [
'FB',
'GOOGL'
]
prices = []
def get_price(ticker)
h = Hash.new
url = "https://finance.google.com/finance"
q_string = "?q=#{ticker}"
response = HTTParty.get(url + q_string)
parsed = Nokogiri::HTML(response.body)
price = parsed.css('span.pr').text.to_f
puts "#{ticker} @ #{price}"
h[ticker] = price
return h
end
for stock_symbol in tickers
prices << get_price(stock_symbol)
end
p prices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment