yfinance: Getting this Error: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Hallo I am keep getting an Error for different scripts her an exampl script:
`tickers = gettickers(‘mdax’)
days = 30
returns, symbols = [], []
klines = {}
for ticker in tickers[]:
klines[ticker] = yf.download(ticker.upper(), interval=‘1d’, period=f’{days}d’, threads=False)
if len(klines[ticker]) > 0:
cumret = (pd.DataFrame(klines[ticker])[‘Close’].astype(float).pct_change() + 1).prod() - 1
returns.append(cumret)
symbols.append(ticker)
retdf = pd.DataFrame(returns, index=symbols, columns=[‘Return’])` print(retdf.Return.nlargest(3))
I am getting an Error thrown for different stocks each time. Did yahoo change its site? Is there a fix for the problem? This is the Error: Traceback (most recent call last): File “C:\Users\User\Desktop\Python\pythonProject\stock\test_center.py”, line 53, in <module> klines[ticker] = yf.download(ticker.upper(), interval=‘1d’, period=f’{days}d’, threads=False) File “C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\yfinance\multi.py”, line 102, in download data = _download_one(ticker, period=period, interval=interval, File “C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\yfinance\multi.py”, line 181, in download_one return Ticker(ticker).history(period=period, interval=interval, File “C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\yfinance\base.py”, line 179, in history data = data.json() File “C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\models.py”, line 910, in json return complexjson.loads(self.text, **kwargs) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\json_init.py", line 346, in loads return _default_decoder.decode(s) File “C:\Users\User\AppData\Local\Programs\Python\Python39\lib\json\decoder.py”, line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File “C:\Users\User\AppData\Local\Programs\Python\Python39\lib\json\decoder.py”, line 355, in raw_decode raise JSONDecodeError(“Expecting value”, s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Process finished with exit code 1
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 38
Seems like an issue on Yahoo’s end. Temporary fix:
This will keep retrying until it manages to retrieve all the tickers without an error - for me, it took ~10-15 seconds for 16 tickers, but obviously will be worse the more tickers there are.
Would be great if a retry mechanism could be implemented in
yfinance
itself, possibly with sane default values and/or possibility to pass a retries number to the download function, to avoid similar issues in the future.Having the same issue (script was still working correctly yesterday)
I fixed it.
This problem is due to the server blocking http get requests (for example, requests from China mainland). The following is the url for yfinance to download stock data. If you can open it normally to get the data, the download function of yfinance is generally ok, otherwise the corresponding error will be displayed: [https://query2.finance.yahoo.com/v8/finance/chart/^DJI]
I downloaded the json of the corresponding data through vpn, and then modified yfinance to read the cached json file instead of downloading it through http get to solve this problem.
You can try to use the construction (try: … except: …) together with threading=False, as a temporary measure it works for me.
Yahoo randomly returns “HTTP Status 404” instead of JSON. Some kind of query limiter?
ok, i also have a vba script i wrote originally that downloads csv files… that is experiencing the same symptoms so i’m guessing its definitely an issue at yahoos end generating the JSON (i think this uses the JSON outputs).
apologies, I just noticed my typo with threads. I think its running through my code at least now, thanks
thanks, I tried this already, I have a comment from 3 hours ago where I tried the below as well 😦 also have try and excepts show_errors=False, threading=False
Initially, when threads=True, the (try: except 😃 construct did not catch this error, the script started working normally only when threads=False, at least for me.
Thanks. Worked fine for me also
Well at least it is not just myself. Thanks for the tip
Also having the same issue - getting it for different stocks each time. Any solution?
Thanks!