yahoo_fin: get_data() not working as of 2/18/2025

stock_info.get_data() worked fine yesterday, but today it fails.

Python 3.12.8 (main, Dec  7 2024, 10:37:29) [Clang 16.0.0 (clang-1600.0.26.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from yahoo_fin.stock_info import get_data
>>> t = get_data("TSLA")
Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/requests/models.py", line 974, in json
    return complexjson.loads(self.text, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/json/decoder.py", line 338, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/json/decoder.py", line 356, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/kwh/Library/Python/3.12/lib/python/site-packages/yahoo_fin/stock_info.py", line 90, in get_data
    raise AssertionError(resp.json())
                         ^^^^^^^^^^^
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/requests/models.py", line 978, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)```

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 7
  • Comments: 19

Commits related to this issue

Most upvoted comments

Same issue here

Options to address the issue:

  1. modify line 31 in yahoo_fin/stock_info.py to be default_headers = {'User-Agent': 'Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'} , or
  2. modify your scripts to add new_headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'} and then modify all calls to get_data to include new_headers, similar to get_data("TSLA", headers=new_headers)

I think the problem are the default headers from the build_url func, which seem to have pretty outdated drivers. You can pass other headers to the func and it should work:

new_headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'}
print(get_data("TSLA", headers=new_headers))

I am having the same issue… yahoo_fin worked without any problem until february 18 2025. Here is the error:

How to fix this?

Read my comment directly above yours in this thread. It has details on two ways to fix the problem, based on the fix provided by @matlafu