ranaroussi / yfinance

Download market data from Yahoo! Finance's API
https://aroussi.com/post/python-yahoo-finance
Apache License 2.0
12.62k stars 2.25k forks source link

How to catch "invalid crumb" exception from yfinance in Python 3.11.9? #1912

Open arline opened 3 months ago

arline commented 3 months ago

Describe bug

have an excel list of tickers which I'm running through yfinance to get fundamental analysis data. I get 2 errors:

"keyError" exception because some data like 'forwardPE' is not available for a given symbol "404 Client Error: Not Found for url:" when opening the URL I get "Invalid Crumb" I tried to catch the 404 in the first loop and keyError in the 2nd loop... the 404 catch isn't working.

Simple code that reproduces your problem

import requests import time import yfinance as yf import pandas as pd

the_list_include = []

################################################### OPEN THE EXCEL FILE AND COLLECT THE SYMBOLS df = pd.read_excel('company_list_404.xlsx') symbols = df['Symbol'].values.tolist()

for count, symbol in enumerate(symbols): try: stock = yf.Ticker(symbol) the_list_include.append(symbol) except requests.exceptions.HTTPError: continue

for i in the_list_include: try: stock = yf.Ticker(i) PE = stock.info['forwardPE'] except KeyError: print("\n") continue

Debug log

404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/ABC?modules=financialData%2CquoteType%2CdefaultKeyStatistics%2CassetProfile%2CsummaryDetail&corsDomain=finance.yahoo.com&formatted=false&symbol=ABC&crumb=74gwrZgqcIA

Bad data proof

No response

yfinance version

yfinance 0.2.38

Python version

Python 3.11.9

Operating system

Windows 11 Home

alfredaita commented 2 months ago

try this ticker = yf.Ticker('GM') ticker.get_info()['forwardPE']