How to create a stock price watcher using Ruby and Nokogiri Gem. The repository containing the complete code used on this guide will be linked at the end of the article. This guide aims to show what you can do using Ruby, you will probably find better / professional ways to do it. The only thing we need from the table is the data from the table. The data is stored inside the rows of the table (tr) and the table. The data in the table (td) is stored in the table with specific keys.
Vinicius Campos Carvalho
Full Stack Developer
For my very first article, I decided to write about web scratching and how I built a stock watcher using Ruby and Nokogiri Gem.
First of all, some prerequisites to follow this guide:
HTML / CSS basics
The purpose of this guide is to show what you can do using Ruby, you will likely find better / professional ways to do it.
The repository containing the complete code used on this guide will be linked at the end of the article.
First, set up your environment, create a ruby file that will contain our scrapper code.
In this case, I’ll create a file called stock_watcher.rb and add the following code to it.
This can just get the data from the web using URI module and parse it using Nokogiri, it returns a
With our page object, we have a bunch of useful methods in our hands.
Now we need to use our HTML / CSS acknowledgment to define where on the page we want to get the data from. Chrome Developer Tools are a good friend here.
The page in question is https://www.tradingview.com/markets/stocks-usa/market-movers-large-cap/, this page shows us US STOCK MARKET companies ranked by large cap. This page stores the data on the main table as you can see, what we need to find is a way to identify this table.
Nokogiri offers us a method called “css” which allows us to search for specific CSS rules on our
, we can search for specific CSS classes, IDs, etc.
Let’s try to find out what is the class or the ID of the table using the Chrome inspection tool, right click on any item in the table and then inspect, you will find a window similar to that -this :
Scroll down a bit until you find the tag
like this :
As you can see, our table uses the “tv-data-table” class, this will be our target class.
Now that we have our table class, add the following code to stock_watcher.rb:
Now we are walking through our XML subset, in this case these are our rows containing the action data. Then we search each row for the data in the table (td) using the same search method, it returns an array with all the data in the iterated row. We need to delete our text because Nokogiri returns a lot of unnecessary chunks of text inside our strings.
Now you can play around with this code a bit, try printing the contents of the stock you got using “puts stock_content” before the end keyword of the last code and see what you got, that should return something something similar to this:
With this data in hand, who defines what needs to be done next is you, there are thousands of possibilities of what could be done with this, I will give a few examples: store data on a file for verification later, store data on a database to manage it later, even viewing the data on another website (check permissions first), create triggers if the value of a stock matches the price you wish. Like I said, there are a lot of possibilities.
Now, to finish this guide, I’ve created a class to handle the data we got from our last piece of code.
First, create another file on the same folder called stock.rb and add the following code:
A ‘monumental auction battle’ at the Norwich Livestock Market saw £ 6,700 paid for a Limousin bull – the best price at the auction. The impressive two-year-old animal, named Withersdale Pinocchio, broke the previous record of £ 4,200 at the bimonthly sale on Hall Road. The new owner is Will Hemmant of Woodhouse Farms in […]
A fundamental concept in economics is that supply and demand determine the price. The greater the quantity of supply of a product or service available and the less demand there is for it, the lower its price will be. In contrast, things that are rare and in high demand are more expensive. The market reaches […]
TOKYO – (COMMERCIAL THREAD) –Valuence Japan Inc. (Tokyo; Susumu Muguruma, Representative Director and Chairman), member of the Valuence Group (TOKYO: 9270), announced on July 1 the launch of the official version of its market price chart of winning bids, available by subscription. The official version of the service has been upgraded and comes with some […]