From 06fd35121a01365794fd7ca4d5fa2737e889d24a Mon Sep 17 00:00:00 2001 From: Ange Daumal Date: Fri, 19 Jan 2024 22:51:02 +0100 Subject: [PATCH] Fix JSON access to prevent KeyError --- yfinance/scrapers/quote.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/yfinance/scrapers/quote.py b/yfinance/scrapers/quote.py index 1daed5327..1d018359b 100644 --- a/yfinance/scrapers/quote.py +++ b/yfinance/scrapers/quote.py @@ -733,10 +733,11 @@ def _fetch_complementary(self, proxy): json_str = self._data.cache_get(url=url, proxy=proxy).text json_data = json.loads(json_str) - if json_data["timeseries"]["error"] is not None: - raise YFinanceException("Failed to parse json response from Yahoo Finance: " + json_data["error"]) + json_result = json_data.get("timeseries") or json_data.get("finance") + if json_result["error"] is not None: + raise YFinanceException("Failed to parse json response from Yahoo Finance: " + str(json_result["error"])) for k in keys: - keydict = json_data["timeseries"]["result"][0] + keydict = json_result["result"][0] if k in keydict: self._info[k] = keydict[k][-1]["reportedValue"]["raw"] else: