In this article, we will see how to get financial data from Yahoo Finance using Python. We can retrieve company financial information (e.g. financial ratios), as well as historical market data by using this.
Installation:
Let us install them via pip commands
pip install yfinance
Once it is installed, we can import yfinance package in python code We need to pass as an argument of Ticker i.e. the ticker of the company
Note: A stock symbol or a ticker is a unique series of letters assigned to a security for trading purposes. For example:
Below are various programs which depict how to retrieve Financial Data from Yahoo Finance:
- For Amazon, it is “AMZN”
- For Facebook, it is “FB”
- For Google, it is “GOOGL”
Output :

Let us take the results for Facebook and hence using “FB”.
Output :
We can retrieve financial key metrics like Company Sector, Price Earnings Ratio, and Company Beta from the above dictionary of items easily. Let us see the below code.
Company Sector : Communication Services Price Earnings Ratio : 31.029732 Company Beta : 1.286265
Output :

Though we have retrieved few financial key metrics, as it is a dictionary value, we can split that by means of key-value pair.
Output :

We can retrieve historical market prices too and display them.
Even we can have the data for 1d, 5d, 1mo, 3mo, 6mo, 1y, 2y, 5y, 10y, and ytd.
Output :

Let us check out for 6 months
Output :

We have the flexibility to get historical market data for the provided start and end dates too.
Conclusion :
Python is a beautiful versatile language and come along with lot of necessary imports. We can rock with Python by means of its versatility.
Some of my technical articles can be seen at
GeeksForGeeks : https://auth.geeksforgeeks.org/user/priyarajtt/articles