One of our readers asked me how to scrap stock price from the MarketWatch website using Ruby. Many would say – use tools like wget or curl, which makes total sense, and there are many tutorials on those tools. For example this is a good tutorial.
However, I will do something different today, just to bring another tool to your arsenal.
The tool we will use is called watir.
Watir is a web-driver that uses a live browser to go to the page and interact with it. It can do many cool things like fill in the form, get content of the page elements etc.
Here is the code to get stock price from MarketWatch website in your terminal .
Scrapping stock price with watir
First thing we need to do is to install watir gem:
gem install watir
Once watir is installed we will do the following:
- Allow user to pass an argument through command line which in our case will be a stock symbol
- Ask watir to go to the MarketWatch website and grab stock price for the symbol
- Scrap stock price and output to the user
- Handle the use case when element cannot be found, or argument is not passed.
Lets jump straight to the code, which in our case is about 15 lines long:
browser = Watir::Browser.new
puts browser.element(class: 'lastprice').text.gsub( "n", "" )
puts "Please pass a stock symbol"
rescue Watir::Exception::UnknownObjectException => e
puts "This stock symbol is not valid!"
As you see we are getting an argument (ARGV) and if it exists we open watir browser. Watir then opens website, gets CSS class called ‘lastprice’ (I researched class name by inspecting the website) that holds the price, formats the output by stripping out any new lines, and outputs formatted stock price to the user.
We are also handling exceptions when stock symbol is not valid.
Lets save our ruby code into something like – scrapper.rb, and run it:
ruby scrapper.rb aapl
My output is:
$> ruby scrapper.rb aapl
Try it and let me know what you think!