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

HTTP 404 error on Ticker.info in Version: 0.2.43 #2060

Closed bala-v closed 2 months ago

bala-v commented 2 months ago

Describe bug

yahoo api seems to have changed in v10

Simple code that reproduces your problem

In [4]: sp_500 = yf.Tickers('SP500')

In [5]: sp_500
Out[5]: yfinance.Tickers object <SP500>

In [6]: for stock in sp_500.tickers:
   ...:     data = yf.Ticker(stock).info
   ...: 
404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/SP500?modules=financialData%2CquoteType%2CdefaultKeyStatistics%2CassetProfile%2CsummaryDetail&corsDomain=finance.yahoo.com&formatted=false&symbol=SP500&crumb=EMn.pSvuZAp

Debug log

404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/SP500?modules=financialData%2CquoteType%2CdefaultKeyStatistics%2CassetProfile%2CsummaryDetail&corsDomain=finance.yahoo.com&formatted=false&symbol=SP500&crumb=EMn.pSvuZAp

Bad data proof

No response

yfinance version

0.2.43

Python version

3.11.1

Operating system

mac

ValueRaider commented 2 months ago

Where's the bad data proof? Prove Yahoo should be returning something.

bala-v commented 2 months ago

Thanks @ValueRaider. On individual stock it seems to work for example

In [2]: msft = yf.Ticker('MSFT').info

In [3]: msft
Out[3]: 
{'address1': 'One Microsoft Way',
 'city': 'Redmond',
 'state': 'WA',
 'zip': '98052-6399',
 'country': 'United States',

But the previous one on sp500 is returning error and not sure if it is related to your fix in https://github.com/ranaroussi/yfinance/commit/c9680d1396b8517ba4119a046b15dac3eb2c989d

ValueRaider commented 2 months ago

Focus on sp500. Focus on Yahoo.com website.

ericpien commented 2 months ago

@bala-v, did this code ever work before?

  1. yahoo ticker for s&p 500 is ^GSPC
  2. Tickers works by passing through a list / string of tickers, not index name. https://github.com/ranaroussi/yfinance/wiki/Tickers
bala-v commented 2 months ago

Thanks @ericpien and @ValueRaider I see the problem. I think code should have never worked.

ValueRaider commented 2 months ago

@bala-v How can the bug form be improved? It currently says this near top:

Then visit finance.yahoo.com and confirm they have the data you want

bala-v commented 2 months ago

@ValueRaider I would think a handy addition to the bug template would be a curl script to make the same call that users can run outside to see if api is returning and if not it would lead them to infer that it could be the ticker or api call. The pdb function of ipython doesn't allow for tracing the actual request/response. The curl script below is what I tried to simulate the problem but gives a valid unauthorized error due to crumb being incorrect -

% curl -X GET \
  'https://query2.finance.yahoo.com/v10/finance/quoteSummary/SP500?modules=financialData%2CquoteType%2CdefaultKeyStatistics%2CassetProfile%2CsummaryDetail&corsDomain=finance.yahoo.com&formatted=false&symbol=SP500&crumb=gOzcFykBslr' \
  -H 'authority: query2.finance.yahoo.com' \
  -H 'pragma: no-cache' \
  -H 'cache-control: no-cache' \
  -H 'sec-ch-ua: "Google Chrome";v="93", "Not/Advanced";v="1", "Chromium";v="93"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "Windows"' \
  -H 'upgrade-insecure-requests: 1' \
  -H 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36' \
  -H 'accept: */*' \
  -H 'sec-fetch-site: same-site' \
  -H 'sec-fetch-mode: no-cors' \
  -H 'sec-fetch-dest: empty' \
  -H 'accept-language: en-US,en;q=0.9' \
  --compressed

{"finance":{"result":null,"error":{"code":"Unauthorized","description":"Invalid Crumb"}}}%     
ValueRaider commented 2 months ago

How would the curl script know what Ticker attribute the user is requesting? Seems complex. I thought just visiting finance.yahoo.com is easier.

bala-v commented 2 months ago

How would the curl script know what Ticker attribute the user is requesting? Seems complex. I thought just visiting finance.yahoo.com is easier.

IMO, since yahoo keeps changing stuff under the hood, though it is easier to visit the site, it might be helpful to have some tracing (e.g --debug) that allows for tracing the call that requests library is making underneath.