Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
arline opened this issue Apr 21, 2024 · 1 comment
Open

Comments

@arline
Copy link

arline commented Apr 21, 2024

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants