Get Financial Data from Yahoo Finance with Python
Last Updated :
14 Jul, 2023
This article will show 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 of Yahoo Finance Module in Python
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 company’s ticker.
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 Meta, it is “META”
- For Google, it is “GOOGL”
Retrieving Financial Data from Yahoo Finance
Below are various examples that depict how to retrieve Financial Data from Yahoo Finance:
Example 1: Getting META Financial Information
Let us take the results for Meta and hence use the “META” ticker.
Python3
import yfinance as yahooFinance
GetFacebookInformation = yahooFinance.Ticker( "META" )
print (GetFacebookInformation.info)
|
Output:
META Financial Information
Example 2: Getting META key metrics
We can retrieve financial key metrics like Company Sector, Price Earnings Ratio, and Company Beta from the above dictionary of items quickly.
Python3
import yfinance as yahooFinance
GetFacebookInformation = yahooFinance.Ticker( "META" )
print ( "Company Sector : " , GetFacebookInformation.info[ 'sector' ])
print ( "Price Earnings Ratio : " , GetFacebookInformation.info[ 'trailingPE' ])
print ( " Company Beta : " , GetFacebookInformation.info[ 'beta' ])
|
Output :
Company Sector : Communication Services
Price Earnings Ratio : 29.883211
Company Beta : 1.198099
Example 3: Getting META information in key-value pairs
Though we have retrieved a few financial key metrics, as it is a dictionary value, we can split that by means of key-value pair.
Python3
import yfinance as yahooFinance
GetFacebookInformation = yahooFinance.Ticker( "META" )
for key, value in GetFacebookInformation.info.items():
print (key, ":" , value)
|
Output :
META information in key-value pairs
Example 4: Getting META historic data
We can retrieve historical market prices too and display them.
Python3
import yfinance as yahooFinance
GetFacebookInformation = yahooFinance.Ticker( "META" )
print (GetFacebookInformation.history(period = "max" ))
|
Output :
META historic data
Example 5: Getting all the rows of META historical data
If we want to display all the rows of a ticker symbol, we will use the Python Pandas module and set the set_option() function to display maximum rows.
Python3
import yfinance as yahooFinance
import pandas as pd
GetFacebookInformation = yahooFinance.Ticker( "META" )
pd.set_option( 'display.max_rows' , None )
print (GetFacebookInformation.history(period = "max" ))
|
Output:
META historic data for all rows
Example 6: Getting META information for a particular period of time
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( "META" )
print (GetFacebookInformation.history(period = "6mo" ))
|
Output:
META information for 6 months
Example 7: Getting META information for the provided date range
We have the flexibility to get historical market data for the provided start and end dates too.
Python3
import yfinance as yahooFinance
import datetime
startDate = datetime.datetime( 2019 , 5 , 31 )
endDate = datetime.datetime( 2021 , 1 , 30 )
GetFacebookInformation = yahooFinance.Ticker( "META" )
print (GetFacebookInformation.history(start = startDate,
end = endDate))
|
Output:
META information for a date range
Share your thoughts in the comments
Please Login to comment...