ranaroussi / yfinance

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

404 Error - Yahoo API changed? yf.download(ticker, start='2022-01-01', end='2023-11-30') works but not yf.Ticker(ticker).info ? #1753

Closed richlysakowski closed 9 months ago

richlysakowski commented 9 months ago

Describe bug

404 Client Error:

Has the API changed?

I will try rewriting my client application code to simply the way Yfinance is used to see if that fixes my responses.

Download works, but not the info() method. Here are simple test code functions and response values below.

import yfinance as yf

def test_yfinance(ticker):
    data = yf.download(ticker, start='2022-01-01', end='2023-11-30')
    print(data)

# Test the function with the AAPL ticker
test_yfinance('AAPL')

returns data: [*****100%%**] 1 of 1 completed Open High Low Close Adj Close Volume Date
2022-01-03 177.8300 182.8800 177.7100 182.0100 179.9539 104487900 2022-01-04 182.6300 182.9400 179.1200 179.7000 177.6700 99310400 2022-01-05 179.6100 180.1700 174.6400 174.9200 172.9440 94537600 2022-01-06 172.7000 175.3000 171.6400 172.0000 170.0570 96904000 2022-01-07 172.8900 174.1400 171.0300 172.1700 170.2251 86709100 ... ... ... ... ... ... ... 2023-11-22 191.4900 192.9300 190.8300 191.3100 191.3100 39617700 2023-11-24 190.8700 190.9000 189.2500 189.9700 189.9700 24048300 2023-11-27 189.9200 190.6700 188.9000 189.7900 189.7900 40552600 2023-11-28 189.7800 191.0800 189.4000 190.4000 190.4000 38415400 2023-11-29 190.9000 192.0900 188.9700 189.3700 189.3700 43014200

[480 rows x 6 columns]

########################################################

import yfinance as yf

def test_get_quote_table_yf(ticker):
    quote_table = yf.Ticker(ticker).info
    print(quote_table)

# Test the function with the AAPL ticker
test_get_quote_table_yf('AAPL')

HTTPError Traceback (most recent call last) c:\Users\PowerUser\Desktop\tosapps\KnowYourOptions\Know_Your_Options-CODEmaster\Know_Your_Options\Know_Your_Options.ipynb Cell 33 line 8 5 print(quote_table) 7 # Test the function with the AAPL ticker ----> 8 test_get_quote_table_yf('AAPL')

c:\Users\PowerUser\Desktop\tosapps\KnowYourOptions\Know_Your_Options-CODEmaster\Know_Your_Options\Know_Your_Options.ipynb Cell 33 line 4 3 def test_get_quote_table_yf(ticker): ----> 4 quote_table = yf.Ticker(ticker).info 5 print(quote_table)

File c:\ProgramData\Anaconda3\envs\tos_tda_apps\Lib\site-packages\yfinance\ticker.py:142, in Ticker.info(self) 140 @property 141 def info(self) -> dict: --> 142 return self.get_info()

File c:\ProgramData\Anaconda3\envs\tos_tda_apps\Lib\site-packages\yfinance\base.py:1736, in TickerBase.get_info(self, proxy) 1734 def get_info(self, proxy=None) -> dict: 1735 self._quote.proxy = proxy -> 1736 data = self._quote.info 1737 return data

File c:\ProgramData\Anaconda3\envs\tos_tda_apps\Lib\site-packages\yfinance\scrapers\quote.py:571, in Quote.info(self) 568 @property 569 def info(self) -> dict: 570 if self._info is None: --> 571 self._fetch(self.proxy) 572 self._fetch_complementary(self.proxy) 574 return self._info

File c:\ProgramData\Anaconda3\envs\tos_tda_apps\Lib\site-packages\yfinance\scrapers\quote.py:600, in Quote._fetch(self, proxy) 598 modules = ['financialData', 'quoteType', 'defaultKeyStatistics', 'assetProfile', 'summaryDetail'] 599 params_dict = {"modules": modules, "ssl": "true"} --> 600 result = self._data.get_raw_json( 601 _BASICURL + f"/{self._data.ticker}", params=params_dict, proxy=proxy 602 ) 603 result["quoteSummary"]["result"][0]["symbol"] = self._data.ticker 604 query1_info = next( 605 (info for info in result.get("quoteSummary", {}).get("result", []) if info["symbol"] == self._data.ticker), 606 None, 607 )

File c:\ProgramData\Anaconda3\envs\tos_tda_apps\Lib\site-packages\yfinance\data.py:75, in TickerData.get_raw_json(self, url, user_agent_headers, params, proxy, timeout) 73 def get_raw_json(self, url, user_agent_headers=None, params=None, proxy=None, timeout=30): 74 response = self.get(url, user_agent_headers=user_agent_headers, params=params, proxy=proxy, timeout=timeout) ---> 75 response.raise_for_status() 76 return response.json()

File c:\ProgramData\Anaconda3\envs\tos_tda_apps\Lib\site-packages\requests\models.py:1021, in Response.raise_for_status(self) 1016 http_error_msg = ( 1017 f"{self.status_code} Server Error: {reason} for url: {self.url}" 1018 ) 1020 if http_error_msg: -> 1021 raise HTTPError(http_error_msg, response=self)

HTTPError: 404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v6/finance/quoteSummary/AAPL?modules=financialData&modules=quoteType&modules=defaultKeyStatistics&modules=assetProfile&modules=summaryDetail&ssl=true

Simple code that reproduces your problem

import yfinance as yf

def test_yf_ticker_info(ticker):
    try:
        info = yf.Ticker(ticker).info
        print(info)
    except Exception as e:
        print(f"An error occurred: {e}")

# Test the function with the AAPL ticker
test_yf_ticker_info('AAPL')

Returns the following result:

An error occurred: 404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v6/finance/quoteSummary/AAPL?modules=financialData&modules=quoteType&modules=defaultKeyStatistics&modules=assetProfile&modules=summaryDetail&ssl=true

Debug log

Debugging yfinance

import requests_cache session = requests_cache.CachedSession('yfinance.cache') session.headers['User-agent'] = 'my-program/1.0' ticker = yf.Ticker('msft', session=session)

The scraped response will be stored in the cache

ticker.actions

  | Dividends | Stock Splits -- | -- | -- 0.0000 | 2.0000 0.0000 | 2.0000 0.0000 | 1.5000 0.0000 | 1.5000 0.0000 | 2.0000 ... | ... 0.6800 | 0.0000 0.6800 | 0.0000 0.6800 | 0.0000 0.6800 | 0.0000 0.7500 | 0.0000

89 rows × 2 columns

Bad data proof

No response

yfinance version

'0.2.31'

Python version

Python 3.11.6

Operating system

Windows 10 Professional

bot-unit commented 9 months ago

pip install yfinance --upgrade --no-cache-dir