masterslowpoke

stock_scraper.rb

Aug 6th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.74 KB | None | 0 0
  1. require 'httparty'
  2. require 'nokogiri'
  3.  
  4. loop do
  5.     puts "Enter a stock identifier. Escape to exit."
  6.     stock_ticker = gets.chomp
  7.    
  8.     if stock_ticker == "\e"
  9.         break
  10.     end
  11.     puts ""
  12.  
  13.     response = HTTParty.get("http://finance.yahoo.com/q?s=#{stock_ticker.upcase}")
  14.     dom = Nokogiri::HTML(response.body)
  15.     stock_span = dom.xpath("//span[@id='yfs_l84_#{stock_ticker.downcase}']").first
  16.     stock_prev = dom.xpath("//td[@class='yfnc_tabledata1']").first
  17.     stock_open = dom.xpath("//td[@class='yfnc_tabledata1']")[1]
  18.    
  19.     print "Stock \"#{stock_ticker.upcase}\""
  20.    
  21.     if(stock_span.nil?)
  22.         puts " cannot be found."
  23.     else
  24.         printf ": PREV CLOSE: $%.2f; OPEN - $%.2f; CURR: $%.2f", stock_prev.content, stock_open.content, stock_span.content
  25.     end
  26.  
  27.     puts ""
  28. end
Advertisement
Add Comment
Please, Sign In to add comment