Get Inventory Worth from Website online in Terminal

One in all our readers requested me methods to scrap inventory value from the  MarketWatch web page the usage of Ruby. Many would say – use  equipment like wget or curl, which makes overall sense, and there are lots of tutorials on the ones equipment. As an example this is a superb educational.

Then again, I can do one thing other nowadays, simply to convey every other device for your arsenal.

The device we will be able to use is named watir.

Watir is a web-driver that  makes use of a are living browser to visit the web page  and have interaction with it. It might probably do many cool such things as fill within the shape, get content material of the web page components and many others.

For the ones of you who’re aware of Selenium – watir is simply every other layer on best of Selenium.

Here’s the code to get inventory value from MarketWatch web page for your terminal .

Scrapping inventory value with watir

Very first thing we want to do is to put in watir gem:

gem set up watir

As soon as watir is put in we will be able to do the next:

  1.  Permit person to cross a controversy via command line which in our case might be a inventory image
  2. Ask watir to visit the MarketWatch web page and take hold of inventory value for the emblem
  3.  Scrap inventory value and output to the person
  4.  Take care of the use case when part can’t be discovered, or argument isn’t handed.

We could leap instantly to the code, which in our case is ready 15 traces lengthy:

require 'watir'

  if ARGV[0]
    browser =
    places browser.part(magnificence: 'lastprice').textual content.gsub( "n", "" )
    places "Please cross a inventory image"
rescue Watir::Exception::UnknownObjectException => e
  places "This inventory image isn't legitimate!"

As you spot we’re getting a controversy (ARGV[0]) and if it exists we open watir browser. Watir then opens web page,  will get CSS magnificence referred to as ‘lastprice’ (I researched magnificence title by way of analyzing the web page) that holds the fee, codecs the output by way of stripping out any new traces, and outputs formatted inventory value to the person.

We also are dealing with exceptions when inventory image isn’t legitimate.

We could save our ruby code into one thing like – scrapper.rb, and run it:

ruby scrapper.rb aapl

My output is:

$> ruby scrapper.rb aapl

Take a look at it and let me know what you suppose!


Thank you for putting in the Backside of each submit plugin by way of Corey Salzano. Touch me if you wish to have customized WordPress plugins or web page design.

Anatoly Spektor

IT Advisor with 6 years enjoy in Device Building and IT Management. Participated in such tasks as Eclipse IDE, Large Blue Button, Toronto 2015 Panam Video games.

Leave a Comment

Your email address will not be published. Required fields are marked *