Repository: ranaroussi/yfinance

Issue:
utils.py: list index out of range
There is a strange behaviour with yfinance 0.1.94 when I try to read ticker "G7W.DU":
Sometimes it works and sometimes the utils.py gets a list index out of range error.

What I expect (and sometimes works):
```
$ python
Python 3.10.9 (main, Dec 11 2022, 14:50:46) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import yfinance as yf
>>> t = "G7W.DU"
>>> ticker = yf.Ticker(t)
>>> ticker.info["regularMarketPrice"]
97
```

What I often get:
```
$ python
Python 3.10.9 (main, Dec 11 2022, 14:50:46) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import yfinance as yf
>>> t = "G7W.DU"
>>> ticker = yf.Ticker(t)
>>> ticker.info["regularMarketPrice"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/foo/.local/lib/python3.10/site-packages/yfinance/ticker.py", line 147, in info
    return self.get_info()
  File "/home/foo/.local/lib/python3.10/site-packages/yfinance/base.py", line 742, in get_info
    self._get_info(proxy)
  File "/home/foo/.local/lib/python3.10/site-packages/yfinance/base.py", line 424, in _get_info
    data = utils.get_json(ticker_url, proxy, self.session)
  File "/home/foo/.local/lib/python3.10/site-packages/yfinance/utils.py", line 205, in get_json
    json_str = html.split('root.App.main =')[1].split(
IndexError: list index out of range
```

There seems to be something special with G7W.DU because I only get the error there, so far. I tried 5 tickers so far and only that one creates this error.


Discussion:
okay I will look into this issue

Resolving patch:
diff --git a/yfinance/utils.py b/yfinance/utils.py
--- a/yfinance/utils.py
+++ b/yfinance/utils.py
@@ -202,6 +202,9 @@ def get_json(url, proxy=None, session=None):
     session = session or _requests
     html = session.get(url=url, proxies=proxy, headers=user_agent_headers).text
 
+    if not "root.App.main =" in html:
+        return {}
+
     json_str = html.split('root.App.main =')[1].split(
         '(this)')[0].split(';\n}')[0].strip()
     data = _json.loads(json_str)


Test patch:
diff --git a/test_yfinance.py b/test_yfinance.py
--- a/test_yfinance.py
+++ b/test_yfinance.py
@@ -20,8 +20,15 @@
 session = None
 import requests_cache ; session = requests_cache.CachedSession("yfinance.cache", expire_after=24*60*60)
 
-symbols = ['MSFT', 'IWO', 'VFINX', '^GSPC', 'BTC-USD']
-tickers = [yf.Ticker(symbol, session=session) for symbol in symbols]
+# Good symbols = all attributes should work
+good_symbols = ['MSFT', 'IWO', 'VFINX', '^GSPC', 'BTC-USD']
+good_tickers = [yf.Ticker(symbol, session=session) for symbol in good_symbols]
+# Dodgy symbols = Yahoo data incomplete, so exclude from some tests
+dodgy_symbols = ["G7W.DU"]
+dodgy_tickers = [yf.Ticker(symbol, session=session) for symbol in dodgy_symbols]
+symbols = good_symbols + dodgy_symbols
+tickers = good_tickers + dodgy_tickers
+# Delisted = no data expected but yfinance shouldn't raise exception
 delisted_symbols = ["BRK.B", "SDLP"]
 delisted_tickers = [yf.Ticker(symbol, session=session) for symbol in delisted_symbols]
 
@@ -118,8 +125,7 @@ def test_attributes_nofail(self):
             ticker.earnings_dates
 
     def test_holders(self):
-        for ticker in tickers:
-            assert(ticker.info is not None and ticker.info != {})
+        for ticker in good_tickers:
             assert(ticker.major_holders is not None)
             assert(ticker.institutional_holders is not None)