Fundamental Analysis of Stocks using Python

Technical analysis and fundamental analysis are the two main techniques to analyse any financial market. Technical analysis looks at the trends and patterns in the past data whereas the fundamental analysis looks at the economic and financial factors of a company.
In this article we shall be performing the fundamental analysis of some of the stocks programmatically. To check out on the technical analysis, refer to this blog: Beginners Guide to Technical Analysis in Python for Algorithmic Trading
Before jumping into the Python code let us first touch upon the basics of fundamental analysis and web scraping.
WHAT IS FUNDAMENTAL ANALYSIS?
Fundamental analysis guides us in reaching a decision point on whether a particular company’s stock is worth buying or not and if its stock price has the right valuation. To perform this analysis it uses the company level factors such as the company’s market capitalization, balance sheet, profit/loss statement, shareholder pattern, cash flow etc
Most of the information required for the fundamental analysis is directly available on various websites. In this article, we will be picking one such webpage from where this information shall be scraped and used to perform the fundamental analysis.
WEB SCRAPING
Copying the data manually from the website is a cumbersome task. Web scraping is a technique that automates this process by extracting data from a website or an online source. Once the required data is pulled from the website it can then be used to perform any kind of data analysis or data manipulation activity.
In this article we will be using ‘Beautiful Soup’ which is a Python package for pulling data from HTML and XML files. It creates a parse tree of the complex HTML document which is then used to read the data.
Refer to this page to get an overview of Beautiful Soup.
Steps involved in web scraping
STEP 1: Identify the url to pull the data
STEP 2: Inspect the page to understand its HTML structure
STEP 3: Write the code to pull the required data from the HTML
Scraping data from other websites
A note of caution on using other websites for web scraping. Please ensure that only publicly available data is scraped.
In order to control the traffic, some websites define the robots.txt file. This gives an indication on the urls that should not be used for web scraping from that particular website. For more details on robots.txt, refer here.
LET’S SCRAPE IT!!
With most of the data required for fundamental analysis directly available on the web, it is easer to scrape this information and use it for further analysis.
In this article we will be considering stocks from the NSE and using the webpage from screener.in to extract the fundamental details of a particular stock. Same logic can be applied for any of the other webpages as well. The code might require minor changes to align with the html structure of that webpage.
The intention of this article is to provide the starter code for someone to quickly get onboard with their own analysis strategy. Hence the scope is limited to fetching some of the basic information and on applying a simple BUY/WAIT strategy.
FETCHING THE FUNDAMENTAL DATA
For this analysis we will be considering the following IT stocks from the National Stock Exchange (NSE): INFY, HCLTECH, LTI, TCS, LTTS, WIPRO
Infosys — Fetch the market cap using web scraping
As an example let us try to extract the market cap for the Infosys stock from the screener webpage using the 3 steps outlined earlier.
STEP 1: Identify the url to pull the data
URL: https://www.screener.in/company/INFY

STEP 2: Inspect the page to understand its HTML structure
Using the browser inspect the page (right click on the page and choose inspect option). The HTML structure of the page can be studied here.
For this example, the HTML page looks like below.

As seen in the HTML structure, the market cap is present within the div class “company-ratios”. Inside this class, it is inside an ul-element (Unordered List Element) with id “top-ratios”. Inside this the market cap is available as a simple span element.
STEP 3: Write the code to pull the required data from the HTML
Using the beautiful soup package, the contents of the given url can be read into a tree object. It can then be traversed till we reach the required data.
Using the below code, the market cap of the Infy stock can be extracted from screener’s webpage.











