JECSand / yahoofinancials

A powerful financial data module used for pulling data from Yahoo Finance. This module can pull fundamental and technical data for stocks, indexes, currencies, cryptos, ETFs, Mutual Funds, U.S. Treasuries, and commodity futures.
https://pypi.python.org/pypi/yahoofinancials
MIT License
896 stars 214 forks source link

get_key_statistic_data: 401 error #173

Closed The70sWoodsman closed 6 months ago

The70sWoodsman commented 6 months ago

When calling the get_key_statistics_data() method, e.g.YahooFinancials('AAPL').get_key_statistics_data(), I get a 401 error with the following message:

{
    "finance": {
        "result": null,
        "error": {
            "code": "Unauthorized",
            "description": "Invalid Crumb"
        }
    }
}

I'm running v1.2 . Other calls tested, such as get_price_history(), seem to be ok.

bjosun commented 6 months ago

Same error again for me too. Worked fine a couple of times but after a few runs it does not get the crumb. Tested with the latest 1.20 and added a print out to see what was the exception, it ran for a very long time.

def _retry_create_dict_ent(self, up_ticker, statement_type, tech_type, report_name, hist_obj):
        i = 0
        while i < 250:
            try:
                out = self._create_dict_ent(up_ticker, statement_type, tech_type, report_name, hist_obj)
                return out
            except Exception as e:
                print(e)
                time.sleep(random.randint(2, 10))
                i += 1
                continue
 Clang 6.0 (clang-600.0.57)] on darwin
 Type "help", "copyright", "credits" or "license" for more information.
 >>> from yahoofinancials import YahooFinancials
 >>> YahooFinancials('AAPL').get_key_statistics_data()
 Server replied with server error code, HTTP 401 code while opening the url: 
 https://query1.finance.yahoo.com/v10/finance/quoteSummary/aapl? 
 modules=defaultKeyStatistics&formatted=False&lang=en-US&region=US&corsDomain=finance.yahoo.com
 Server replied with server error code, HTTP 401 code while opening the url: 
 https://query1.finance.yahoo.com/v10/finance/quoteSummary/aapl? modules=defaultKeyStatistics&formatted=False&lang=en-US&region=US&corsDomain=finance.yahoo.com
 Server replied with server error code, HTTP 401 code while opening the url: 
 https://query1.finance.yahoo.com/v10/finance/quoteSummary/aapl? modules=defaultKeyStatistics&formatted=False&lang=en-US&region=US&corsDomain=finance.yahoo.com

Started to work after tying out changing in the session.py from finance.yahoo.com to fc.yahoo.com response = self._session.get( url='https://finance.yahoo.com', headers=self.user_agent_headers, proxies=proxy, timeout=timeout, allow_redirects=True)

And it's still working after changing it back, som maybe a glitch in the yahoo finance api.

The70sWoodsman commented 6 months ago

That is bizarre, but I agree. I just retested and I'm getting the correct JSON back 🤷. Not a bug.