ranaroussi / yfinance

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

Exception: yfinance failed to decrypt Yahoo data response #1426

Closed Rapid1898-code closed 1 year ago

Rapid1898-code commented 1 year ago

Hello - when i run the folklowing code:

import yfinance as yf
import time

if __name__ == '__main__':
  ticker = ["AAPL", "XYZ", "MSFT"]
  while True:
    for t in ticker:
      data = yf.Ticker(t)
      if data.info == None:
        print(f"Nothing found for ticker")
      else:
        print(f"Date read for {t}")
      time.sleep(1)

I get (earlier or later) the following error message -

$ python test.py
Traceback (most recent call last):
  File "G:\DEV\Python-Diverses\YFinance\test.py", line 9, in <module>
    if data.info == None:
  File "G:\DEV\.venv\selAll\lib\site-packages\yfinance\ticker.py", line 138, in info
    return self.get_info()
  File "G:\DEV\.venv\selAll\lib\site-packages\yfinance\base.py", line 1475, in get_info
    data = self._quote.info
  File "G:\DEV\.venv\selAll\lib\site-packages\yfinance\scrapers\quote.py", line 95, in info
    self._scrape(self.proxy)
  File "G:\DEV\.venv\selAll\lib\site-packages\yfinance\scrapers\quote.py", line 124, in _scrape
    json_data = self._data.get_json_data_stores(proxy=proxy)
  File "G:\DEV\.venv\selAll\lib\site-packages\yfinance\data.py", line 41, in wrapped
    return func(*args, **kwargs)
  File "G:\DEV\.venv\selAll\lib\site-packages\yfinance\data.py", line 311, in get_json_data_stores
    stores = decrypt_cryptojs_aes_stores(data, keys)
  File "G:\DEV\.venv\selAll\lib\site-packages\yfinance\data.py", line 162, in decrypt_cryptojs_aes_stores
    raise Exception("yfinance failed to decrypt Yahoo data response")
Exception: yfinance failed to decrypt Yahoo data response

Why is that - and why is it happening in the line with this statement: if data.info == None:

Is there anything i can do for preventing such errors?

ValueRaider commented 1 year ago

@Rapid1898-code If you maintain this software for clients, you should really know how to read Python. yf.Ticker.info triggers a fetch that ultimately fails to decrypt.

Rapid1898-code commented 1 year ago

Hello - i don´t understand what is wrong with checking if there is some result with: if data.info == None: Is this in any way wrong handling? And why is it outputting an error with "yfinance failed to decrypt Yahoo data response"?

ValueRaider commented 1 year ago

info isn't a variable. It's a property function, see the source code. It behaves like a variable, and to initialise it requires decrypting data returned by Yahoo. That is failing, clearly. #1407

Rapid1898-code commented 1 year ago

So to correct way is to allways use a try and except logic when accessing data from info. Do i understand this correct?

ValueRaider commented 1 year ago

No. If decryption is failing, go outside for a walk because there's nothing you can do.

Rapid1898-code commented 1 year ago

Hello - i still not get it - I have the situation that i read several tickers - ticker by ticker (eg. 100 tickers) And sometimes as described i get this error after 10 tickers or so.

But with a workaround it seems to work - So i make a try and except and when i get this exception i wait for eg. 5 seconds and then try it again. Most of the time after this 5 seconds access to info works fine.

So it seems for me that there is something i can do. (or it would be a very short walk...)

ValueRaider commented 1 year ago

Interesting, I thought decryption had 100% broken for everyone. Certainly has for me. But it's ultimately your decision what to do when yfinance can't decrypt Yahoo's data.

Rapid1898-code commented 1 year ago

so you mean generally there is sometimes no way to get data from yfinance you think?

ValueRaider commented 1 year ago

Read #1407