I’m new to yfinance and am just trying to write a code in which you input a stock ticker and output its current price.
This is all I’ve done so far:
import pandas as pd
import pandas_datareader as data
import numpy as np
import seaborn
import matplotlib.pyplot as plt
import datetime as dt
import googlefinance as gf
import yfinance as yf
from yahoo_finance import Share
msft = yf.Ticker("MSFT")
# get all stock info
print(msft.info)
But currently I just get this as a result:
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v6/finance/quoteSummary/MSFT?modules=financialData&modules=quoteType&modules=defaultKeyStatistics&modules=assetProfile&modules=summaryDetail&ssl=true
I’ve tried restarting my computer and everything. Nothing changes, it still won’t produce the expected output.
>Solution :
The error you are encountering is due to the fact that the package you are using, yahoo_finance, is outdated and no longer supported.
To get the current price of a stock using the yfinance package, you can modify your code as follows:
import yfinance as yf
# Input the stock ticker
ticker = input("Enter the stock ticker symbol: ")
# Create a Ticker object
stock = yf.Ticker(ticker)
# Get the current price
current_price = stock.history().tail(1)['Close'][0]
# Print the current price
print("Current price of", ticker, "is:", current_price)
This code will prompt you to enter the stock ticker symbol and will then output its current price.