ranaroussi / yfinance

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

.news will raise JSONDecodeError #2015

Closed cyc00518 closed 3 months ago

cyc00518 commented 3 months ago

Describe bug

When I use .news, it raise JSONDecodeError.

But it worked well before.

Simple code that reproduces your problem

import yfinance as yf
ticker = yf.Ticker('SPY')
print(ticker.news)

Debug log

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
File ~/.local/lib/python3.8/site-packages/requests/models.py:971, in Response.json(self, **kwargs)
    970 try:
--> 971     return complexjson.loads(self.text, **kwargs)
    972 except JSONDecodeError as e:
    973     # Catch JSON-related errors and raise as requests.JSONDecodeError
    974     # This aliases json.JSONDecodeError and simplejson.JSONDecodeError

File /opt/conda/lib/python3.8/json/__init__.py:357, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    354 if (cls is None and object_hook is None and
    355         parse_int is None and parse_float is None and
    356         parse_constant is None and object_pairs_hook is None and not kw):
--> 357     return _default_decoder.decode(s)
    358 if cls is None:

File /opt/conda/lib/python3.8/json/decoder.py:337, in JSONDecoder.decode(self, s, _w)
    333 """Return the Python representation of ``s`` (a ``str`` instance
    334 containing a JSON document).
    335 
    336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338 end = _w(s, end).end()

File /opt/conda/lib/python3.8/json/decoder.py:355, in JSONDecoder.raw_decode(self, s, idx)
    354 except StopIteration as err:
--> 355     raise JSONDecodeError("Expecting value", s, err.value) from None
    356 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

JSONDecodeError                           Traceback (most recent call last)
Input In [16], in <cell line: 3>()
      1 import yfinance as yf
      2 ticker = yf.Ticker('SPY')
----> 3 print(ticker.news)

File ~/.local/lib/python3.8/site-packages/yfinance/ticker.py:262, in Ticker.news(self)
    260 @property
    261 def news(self) -> list:
--> 262     return self.get_news()

File ~/.local/lib/python3.8/site-packages/yfinance/base.py:513, in TickerBase.get_news(self, proxy)
    509 if "Will be right back" in data.text:
    510     raise RuntimeError("*** YAHOO! FINANCE IS CURRENTLY DOWN! ***\n"
    511                        "Our engineers are working quickly to resolve "
    512                        "the issue. Thank you for your patience.")
--> 513 data = data.json()
    515 # parse news
    516 self._news = data.get("news", [])

File ~/.local/lib/python3.8/site-packages/requests/models.py:975, in Response.json(self, **kwargs)
    971     return complexjson.loads(self.text, **kwargs)
    972 except JSONDecodeError as e:
    973     # Catch JSON-related errors and raise as requests.JSONDecodeError
    974     # This aliases json.JSONDecodeError and simplejson.JSONDecodeError
--> 975     raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Bad data proof

No response

yfinance version

0.2.41

Python version

No response

Operating system

No response

Suranjandas7 commented 3 months ago

facing the same issue

cyc00518 commented 3 months ago

@Suranjandas7 It's solved by https://github.com/ranaroussi/yfinance/pull/2021.