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:
- Permit person to cross a controversy via command line which in our case might be a inventory image
- Ask watir to visit the MarketWatch web page and take hold of inventory value for the emblem
- Scrap inventory value and output to the person
- 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' start if ARGV browser = Watir::Browser.new browser.goto(" places browser.part(magnificence: 'lastprice').textual content.gsub( "n", "" ) browser.shut else places "Please cross a inventory image" finish rescue Watir::Exception::UnknownObjectException => e places "This inventory image isn't legitimate!" browser.shut finish
As you spot we’re getting a controversy (ARGV) 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 $102.79
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.