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:
In what appears to be a counterintuitive strategy, seven drugs have stacked up in the anti-PD- (L) 1 market so far, all at roughly the same list price. In the absence of proper price competition, how do companies like Sanofi’s Regeneron and Libtayo differentiate themselves? Maybe try to discount, thinks a leading analyst. Ronny gal […]
Whale spotted: How whales determine the price of the crypto market Did you know that the largest wallet contains over 168,791 Bitcoins? To put it in perspective, it’s worth over $ 10,645,817,161 as of April 14, 2021. Another dormant Bitcoin address has over 80,000 BTC, worth over $ 750 million. The “crypto whales” are among […]
The major Ethereum upgrade, codenamed as London, went live earlier today, helping the record price of Ethereum rise 20% over the past week and sending bitcoin back more of $ 40,000. The surge in the price of ethereum, pushing ether tokens towards $ 3,000, pushed the combined crypto market up to nearly $ 1.7 trillion, […]