ranaroussi / yfinance

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

ticker.info returning value for non existing tickers #1905

Open subhammoda opened 3 months ago

subhammoda commented 3 months ago

Describe bug

The following code used to work fine previously while trying to check if a ticker exists as the info would return 404 error, but not info returns 'trailingPegRatio' value even for non-existing tickers.

Simple code that reproduces your problem

import yfinance as yf

yf.enable_debug_mode()

symbol = "FAKE"
ticker = yf.Ticker(symbol)
info = None

try:
  info = ticker.info
except:
  print("Ticker {} probably does not exist.".format(symbol))
else:  
  print("Info of {}:{}".format(symbol, info))

Debug log

DEBUG get_raw_json(): https://query2.finance.yahoo.com/v10/finance/quoteSummary/FAKE DEBUG Entering get() DEBUG url=https://query2.finance.yahoo.com/v10/finance/quoteSummary/FAKE DEBUG params={'modules': 'financialData,quoteType,defaultKeyStatistics,assetProfile,summaryDetail', 'corsDomain': 'finance.yahoo.com', 'formatted': 'false', 'symbol': 'FAKE'} DEBUG Entering _get_cookie_and_crumb() DEBUG cookie_mode = 'basic' DEBUG Entering _get_cookie_and_crumb_basic() DEBUG loaded persistent cookie DEBUG reusing cookie DEBUG crumb = 'SYvgBDKYViL' DEBUG Exiting _get_cookie_and_crumb_basic() DEBUG Exiting _get_cookie_and_crumb() DEBUG response code=404 DEBUG toggling cookie strategy basic -> csrf DEBUG Entering _get_cookie_and_crumb() DEBUG cookie_mode = 'csrf' DEBUG Entering _get_crumb_csrf() DEBUG Failed to find "csrfToken" in response DEBUG Exiting _get_crumb_csrf() DEBUG toggling cookie strategy csrf -> basic DEBUG Entering _get_cookie_and_crumb_basic() DEBUG loaded persistent cookie DEBUG reusing cookie DEBUG crumb = 'SYvgBDKYViL' DEBUG Exiting _get_cookie_and_crumb_basic() DEBUG Exiting _get_cookie_and_crumb() DEBUG response code=404 DEBUG Exiting get() ERROR 404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/FAKE?modules=financialData%2CquoteType%2CdefaultKeyStatistics%2CassetProfile%2CsummaryDetail&corsDomain=finance.yahoo.com&formatted=false&symbol=FAKE&crumb=SYvgBDKYViL DEBUG Entering get() DEBUG url=https://query1.finance.yahoo.com/ws/fundamentals-timeseries/v1/finance/timeseries/FAKE?symbol=FAKE&type=trailingPegRatio&period1=1697155200&period2=1712966400 DEBUG params=None DEBUG Entering _get_cookie_and_crumb() DEBUG cookie_mode = 'basic' DEBUG Entering _get_cookie_and_crumb_basic() DEBUG reusing cookie DEBUG reusing crumb DEBUG Exiting _get_cookie_and_crumb_basic() DEBUG Exiting _get_cookie_and_crumb() DEBUG response code=200 DEBUG Exiting get() Traceback (most recent call last): File "/Users/subhammoda/Documents/Projects/FE520_Project/test.py", line 14, in print(f"Info of {t}: {info}") NameError: name 't' is not defined subhammoda@Subhams-Air Projects % /usr/local/bin/python /Users/subhammoda/Documents/Projects/FE520_Project/test.py DEBUG get_raw_json(): https://query2.finance.yahoo.com/v10/finance/quoteSummary/FAKE DEBUG Entering get() DEBUG url=https://query2.finance.yahoo.com/v10/finance/quoteSummary/FAKE DEBUG params={'modules': 'financialData,quoteType,defaultKeyStatistics,assetProfile,summaryDetail', 'corsDomain': 'finance.yahoo.com', 'formatted': 'false', 'symbol': 'FAKE'} DEBUG Entering _get_cookie_and_crumb() DEBUG cookie_mode = 'basic' DEBUG Entering _get_cookie_and_crumb_basic() DEBUG loaded persistent cookie DEBUG reusing cookie DEBUG crumb = 'SYvgBDKYViL' DEBUG Exiting _get_cookie_and_crumb_basic() DEBUG Exiting _get_cookie_and_crumb() DEBUG response code=404 DEBUG toggling cookie strategy basic -> csrf DEBUG Entering _get_cookie_and_crumb() DEBUG cookie_mode = 'csrf' DEBUG Entering _get_crumb_csrf() DEBUG Failed to find "csrfToken" in response DEBUG Exiting _get_crumb_csrf() DEBUG toggling cookie strategy csrf -> basic DEBUG Entering _get_cookie_and_crumb_basic() DEBUG loaded persistent cookie DEBUG reusing cookie DEBUG crumb = 'SYvgBDKYViL' DEBUG Exiting _get_cookie_and_crumb_basic() DEBUG Exiting _get_cookie_and_crumb() DEBUG response code=404 DEBUG Exiting get() ERROR 404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/FAKE?modules=financialData%2CquoteType%2CdefaultKeyStatistics%2CassetProfile%2CsummaryDetail&corsDomain=finance.yahoo.com&formatted=false&symbol=FAKE&crumb=SYvgBDKYViL DEBUG Entering get() DEBUG url=https://query1.finance.yahoo.com/ws/fundamentals-timeseries/v1/finance/timeseries/FAKE?symbol=FAKE&type=trailingPegRatio&period1=1697155200&period2=1712966400 DEBUG params=None DEBUG Entering _get_cookie_and_crumb() DEBUG cookie_mode = 'basic' DEBUG Entering _get_cookie_and_crumb_basic() DEBUG reusing cookie DEBUG reusing crumb DEBUG Exiting _get_cookie_and_crumb_basic() DEBUG Exiting _get_cookie_and_crumb() DEBUG response code=200 DEBUG Exiting get() Info of FAKE:{'trailingPegRatio': None}

Bad data proof

ticker.info should return response 404, and not {'trailingPegRatio': None}

yfinance version

0.2.37

Python version

3.9.12

Operating system

macOS

ValueRaider commented 3 months ago

1774 is only relevant recent change.