Get Financial Data from Yahoo Finance with Python

16 Jun, 2021
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:
For Amazon, it is “AMZN”For Facebook, it is “FB”For Google, it is “GOOGL”
For Amazon, it is “AMZN”
For Facebook, it is “FB”
For Google, it is “GOOGL”
Below are various programs which depict how to retrieve Financial Data from Yahoo Finance: 
Let us take the results for Facebook and hence using “FB”.
Python3
import yfinance as yahooFinance # Here We are getting Facebook financial information# We need to pass FB as argument for thatGetFacebookInformation = yahooFinance.Ticker("FB") # whole python dictionary is printed hereprint(GetFacebookInformation.info)
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.
Python3
import yfinance as yahooFinance  GetFacebookInformation = yahooFinance.Ticker("FB") # display Company Sectorprint("Company Sector : ", GetFacebookInformation.info['sector']) # display Price Earnings Ratioprint("Price Earnings Ratio : ", GetFacebookInformation.info['trailingPE']) # display Company Betaprint(" Company Beta : ", GetFacebookInformation.info['beta'])
Output :
Company Sector :  Communication Services
Price Earnings Ratio :  31.029732
 Company Beta :  1.286265
Though we have retrieved few financial key metrics, as it is a dictionary value, we can split that by means of key-value pair. 
Python3
import yfinance as yahooFinanceGetFacebookInformation = yahooFinance.Ticker("FB") # get all key value pairs that are availablefor key, value in GetFacebookInformation.info.items():    print(key, ":", value)
 
 
Output :
 
 
We can retrieve historical market prices too and display them.
 
Python3
import yfinance as yahooFinance  GetFacebookInformation = yahooFinance.Ticker("FB") # Let us  get historical stock prices for Facebook# covering the past few years.# max->maximum number of daily prices available# for Facebook.# Valid options are 1d, 5d, 1mo, 3mo, 6mo, 1y, 2y,# 5y, 10y and ytd.print(GetFacebookInformation.history(period="max"))
 
 
Output :
 
 
Even we can have the data for 1d, 5d, 1mo, 3mo, 6mo, 1y, 2y, 5y, 10y, and ytd. 
 
Let us check out for 6 months
 
Python3
import yfinance as yahooFinance  GetFacebookInformation = yahooFinance.Ticker("FB") # Valid options are 1d, 5d, 1mo, 3mo, 6mo, 1y,# 2y, 5y, 10y and ytd.print(GetFacebookInformation.history(period="6mo"))
 
 
Output :
 
 
We have the flexibility to get historical market data for the provided start and end dates too.
 
Python3
import yfinance as yahooFinance # in order to specify start date and# end date we need datetime packageimport datetime # startDate , as per our convenience we can modifystartDate = datetime.datetime(2019, 5, 31) # endDate , as per our convenience we can modifyendDate = datetime.datetime(2021, 1, 30)GetFacebookInformation = yahooFinance.Ticker("FB") # pass the parameters as the taken dates for start and endprint(GetFacebookInformation.history(start=startDate,                                     end=endDate))
Output :
simranarora5sos
Picked
python-modules
Web-scraping
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.