In this Python article we want to learn How to Find Google Stock Price with Python, for this we are going to use Yahoo Finance API (yfinance) library, so first of all let’s talk about Yahoo Finance API.
What is Yahoo Finance API (yfinance) ?
yfinance is popular Python library that provides easy API for accessing historical market data and live financial data from Yahoo Finance. it allows users to fetch data for stock quotes, options, mutual funds, exchange traded funds (ETFs) and cryptocurrencies. yfinance is built on top of the Pandas library and is widely used for financial analysis and data science.
How to Find Google Stock Price with Python
First of all we need to install this library and you can use pip for this
1 |
pip install yfinance |
Now this is our code
1 2 3 4 5 6 7 |
import yfinance as yf #Get Google stock price with yfinance goog = yf.Ticker("GOOGL") stock_price = goog.history(period="5d")["Close"] print(stock_price) |
In the above code we have created Ticker object for the Google stock by passing its ticker symbol GOOGL as an argument to the Ticker constructor. history method of the Ticker object is then called with the period parameter set to 5d to retrieve historical stock prices for the past 5 days. and final line of code assigns the Close prices to the stock_price variable and prints it to the console.
Run the code and this is the result
Now let’s visualize the data using Matplotlib
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import yfinance as yf import matplotlib.pyplot as plt #Get the Google stock price with yfinance goog = yf.Ticker("GOOGL") stock_price = goog.history(period="5d")["Close"] #Visualize the stock price with matplotlib plt.plot(stock_price) plt.title("Google Stock Price") plt.xlabel("Date") plt.ylabel("Price") plt.show() |
This code will first retrieve the Google stock price using yfinance API, after that we use matplotlib to create simple line graph of the stock price over the past 5 days. resulting graph will be displayed in new window when you run the code.
This will be the result
Learn More on Python GUI
- Qt Designer Tool in PyQt6
- How to Use Stylesheets in Python PyQt6
- Event Handling in Python and PyQt6
- Responsive Applications with PyQt6 Multithreading
- How to Deploy PyQt6 Applications
- How to Build Web Applications with PyQt6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python and PyQt6
- How to Use Stylesheets in Python PyQt6