ranaroussi / yfinance

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

ticker.info.get is giving 404 Client Error since 30-OCT-2023 afternoon #1732

Closed rmothkuri closed 11 months ago

WillMo2198 commented 11 months ago

Same here. Still going this morning. This is the error code I got:

Traceback (most recent call last): File "main.py", line 167, in <module> main() File "main.py", line 145, in main last_price = current_price(ticker) File "main.py", line 24, in current_price price = yf.Ticker(ticker).info['currentPrice'] File "venv/lib/python3.9/site-packages/yfinance/ticker.py", line 142, in info return self.get_info() File "venv/lib/python3.9/site-packages/yfinance/base.py", line 1736, in get_info data = self._quote.info File "venv/lib/python3.9/site-packages/yfinance/scrapers/quote.py", line 571, in info self._fetch(self.proxy) File "venv/lib/python3.9/site-packages/yfinance/scrapers/quote.py", line 600, in _fetch result = self._data.get_raw_json( File "venv/lib/python3.9/site-packages/yfinance/data.py", line 75, in get_raw_json response.raise_for_status() File "venv/lib/python3.9/site-packages/requests/models.py", line 1021, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v6/finance/quoteSummary/MVIS?modules=financialData&modules=quoteType&modules=defaultKeyStatistics&modules=assetProfile&modules=summaryDetail&ssl=true

KryptoEmman commented 11 months ago

Same here. Trying to open URL in Chrome returns:

_yahoo! Will be right back... Thank you for your patience.

Our engineers are working quickly to resolve the issue._

cestes commented 11 months ago

Same... receiving the following:

Traceback (most recent call last):
  File "ticker.py", line 68, in <module>
    main()
  File "ticker.py", line 34, in main
    match thisstock.info["quoteType"]:
          ^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/yfinance/ticker.py", line 142, in info
    return self.get_info()
           ^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/yfinance/base.py", line 1736, in get_info
    data = self._quote.info
           ^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/yfinance/scrapers/quote.py", line 571, in info
    self._fetch(self.proxy)
  File "/lib/python3.11/site-packages/yfinance/scrapers/quote.py", line 600, in _fetch
    result = self._data.get_raw_json(
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/yfinance/data.py", line 75, in get_raw_json
    response.raise_for_status()
  File "/lib/python3.11/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v6/finance/quoteSummary/%5EDJI?modules=financialData&modules=quoteType&modules=defaultKeyStatistics&modules=assetProfile&modules=summaryDetail&ssl=true
mmplive commented 11 months ago

thisis an example link that yfinance goes to but yahoo throws error: https://query2.finance.yahoo.com/v6/finance/quoteSummary/AAL?modules=financialData&modules=quoteType&modules=defaultKeyStatistics&modules=assetProfile&modules=summaryDetail&ssl=true

xanderrp2 commented 11 months ago

Also getting it.

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

The-Milad-A commented 11 months ago

Same 404 error here - is the yahoo finance API down?

xanderrp2 commented 11 months ago

I'm in a class thats using yfinance, and I seem to be the only person having these problems

elsheikh21 commented 11 months ago

I am also getting the same problem

msft = yf.Ticker("MSFT")
msft_data = msft.info
print(msft_data.get("grossMargins"))
ksteward commented 11 months ago

same here

rmothkuri commented 11 months ago

Is there any alternative to get market_cap and enterprise_value with out using ticker.info

ValueRaider commented 11 months ago

Duplicate issue.

ksteward commented 11 months ago

do you have a link to the duplicate issue?

KryptoEmman commented 11 months ago

Duplicate issue.

Interesting because the other 404 issue that I see that is currently open was submitted 2 weeks ago and I only started to receive this issue this morning.

ValueRaider commented 11 months ago

@KryptoEmman Because USA was safe initially but not anymore. But you knew that because you read the thread ...

xanderrp2 commented 11 months ago

From what I've been able to find, this problem is only happening with the .info function. Sometimes the .info function breaks but it'll get fixed again soon (this has happened before). for now, if you can get buy with .fast_info then you should be ok. We just need to wait for the yfinance.info function to get updated again. (I'm not sure if this is the same problem that everyone else is having). Then you'll need to update yfinance

ksteward commented 11 months ago

ticker.fast_info() and ticker.tast_info are not responding either. Is it available some other way?

ksteward commented 11 months ago

ok, print(yf.Ticker("AAPL").fast_info["last_price"]) works, but there are fewer keys in fast_info[] than the original .info[]

Ali-Hon commented 11 months ago

Same issue when calling: stock_info = stock_info_impl.fetch_single_stock_data(ticker), Anybody found a fix? The fixes in the duplicate didn't do it.

KryptoEmman commented 11 months ago

@AliCodesJava I gave up waiting. Did some digging around and ended up writing my own from broken examples I found out there using LXML and Requests modules grabbing the tables and values from the Summary and Statistics page tabs at their associated URLs (https://ca.finance.yahoo.com/quote/{TICKER}?p={TICKER}&.tsrc=fin-srch and https://ca.finance.yahoo.com/quote/{TICKER}/key-statistics?p={TICKER} respectively) instead of the Yahoo Finance "API" that yfinance and others Yahoo Finance readers typically use and which keep getting blown up by Yahoo from time to time. I find that those tabs give me all of the info I would normally need, but one can easily adapt things to get info from additional tabs. Still working out some kinks in the code and also create and return an ticker object with all the pertinent properties obtained from the values on those tabs (using having to use a ticker_data.get("key_name") from after I return a dictionary object with all the ticker data, which is bit sloppy. Let me know if you're interested and I'll share what I put together somehow.

WillMo2198 commented 11 months ago

@AliCodesJava

I gave up waiting. Did some digging around and ended up writing my own from broken examples I found out there using LXML and Requests modules grabbing the tables and values from the Summary and Statistics page tabs at their associated URLs (https://ca.finance.yahoo.com/quote/{TICKER}?p={TICKER}&.tsrc=fin-srch and https://ca.finance.yahoo.com/quote/{TICKER}/key-statistics?p={TICKER} respectively) instead of the Yahoo Finance "API" that yfinance and others Yahoo Finance readers typically use and which keep getting blown up by Yahoo from time to time. I find that those tabs give me all of the info I would normally need, but one can easily adapt things to get info from additional tabs. Still working out some kinks in the code and also create and return an ticker object with all the pertinent properties obtained from the values on those tabs (using having to use a ticker_data.get("key_name") from after I return a dictionary object with all the ticker data, which is bit sloppy. Let me know if you're interested and I'll share what I put together somehow.

Please share when done, this sounds great!

Ali-Hon commented 11 months ago

@AliCodesJava I gave up waiting. Did some digging around and ended up writing my own from broken examples I found out there using LXML and Requests modules grabbing the tables and values from the Summary and Statistics page tabs at their associated URLs (https://ca.finance.yahoo.com/quote/{TICKER}?p={TICKER}&.tsrc=fin-srch and https://ca.finance.yahoo.com/quote/{TICKER}/key-statistics?p={TICKER} respectively) instead of the Yahoo Finance "API" that yfinance and others Yahoo Finance readers typically use and which keep getting blown up by Yahoo from time to time. I find that those tabs give me all of the info I would normally need, but one can easily adapt things to get info from additional tabs. Still working out some kinks in the code and also create and return an ticker object with all the pertinent properties obtained from the values on those tabs (using having to use a ticker_data.get("key_name") from after I return a dictionary object with all the ticker data, which is bit sloppy. Let me know if you're interested and I'll share what I put together somehow.

I see! Super interested! Would appreciate it if you shared, please!

KryptoEmman commented 11 months ago

@AliCodesJava @WillMo2198 Here you go:

from lxml import html import requests from collections import OrderedDict

class ticker_data_object:

'''
Instantiation of ticker_data_object will set attributes and assign their value(s) from the URL response keys and values
stored to ticker data dictionary passed while using valid attribute names mapped in ticker_data_translator dictionary
to keys retrieved from URL response
'''
def __init__(self, data=None):
    if data is not None:
        for key, value in data.items():
            attribute = [translator_value for translator_key, translator_value in ticker_data_translator.items() if key.startswith(translator_key) == True][0]
            setattr(self, attribute, value)

def get_ticker_data_translator():

'''
Map valid attribute names to use for each key retrived from the URL response

NOTE: Keys below are substrings used to locate actual key names restrieved from URL responses
      some of which may terminate with dynamic info such as a specifica date

'''
return {
        'Current Price': 'current_price',
        'Previous Close': 'previous_close', 
        'Open': 'open_price',
        'Bid': 'bid',
        'Ask': 'ask',
        'Day\'s Range': 'days_range',
        '52 Week Range': 'fifty_two_week_range',
        'Volume': 'volume',
        'Avg. Volume': 'avg_volume',
        'Market Cap': 'market_cap',
        'Beta (5Y Monthly)': 'beta_5y_monthly',
        'PE Ratio (TTM)': 'pe_ratio_ttm',
        'EPS (TTM)': 'eps_ttm',
        'Earnings Date': 'earnings_date',
        'Forward Dividend & Yield': 'forward_dividend_and_yield',
        'Ex-Dividend Date': 'ex_dividend_date',
        '1y Target Est': 'one_year_target_est',
        'Market Cap (intraday)': 'market_cap_intraday',
        'Enterprise Value': 'enterprise_value',
        'Trailing P/E': 'trailing_pe',
        'Forward P/E': 'forward_pe',
        'PEG Ratio (5 yr expected)': 'peg_ratio_5_yr_expected',
        'Price/Sales (ttm)': 'price_to_sales_ttm',
        'Price/Book (mrq)': 'price_to_book_mrq',
        'Enterprise Value/Revenue': 'enterprise_value_to_revenue',
        'Enterprise Value/EBITDA': 'enterprise_value_to_revenue',
        '52-Week Change': 'fifty_two_week_change',
        'S&P500 52-Week Change': 'sp500_52_wee_change',
        '52 Week High': 'fifty_two_week_high',
        '52 Week Low': 'fifty_two_week_low',
        '50-Day Moving Average': 'fifty_day_average',
        '200-Day Moving Average': 'two_hundred_day_moving_average',
        'Avg Vol (3 month)': 'avg_vol_3_month',
        'Avg Vol (10 day)': 'avg_vol_10_day',
        'Shares Outstanding': 'shares_outstanding',
        'Implied Shares Outstanding': 'implied_shares_oustanding',
        'Float': 'float_amount',
        '% Held by Insiders': 'percentage_held_by_insiders',
        '% Held by Institutions': 'percentage_held_by_institutions',
        'Shares Short': 'shares_short',
        'Short Ratio': 'short_ratio',
        'Short % of Float': 'short_percentage_of_float',
        'Short % of Shares Outstanding': 'short_percentage_of_shares_outstanding',
        'Shares Short (prior month': 'shares_short_prior_month',
        'Forward Annual Dividend Rate': 'forward_annual_dividend_rate',
        'Forward Annual Dividend Yield': 'forward_annual_dividend_yield',
        'Trailing Annual Dividend Rate': 'trailing_annual_dividend_rate',
        'Trailing Annual Dividend Yield': 'trailing_annual_dividend_yield',
        '5 Year Average Dividend Yield': 'five_year_average_dividend_yield',
        'Payout Ratio': 'payout_ratio',
        'Dividend Date': 'dividend_date',
        'Ex-Dividend Date': 'ex_dividend_date',
        'Last Split Factor': 'last_split_factor',
        'Last Split Date': 'last_split_date',
        'Fiscal Year Ends': 'fiscal_year_ends',
        'Most Recent Quarter (mrq)': 'most_recent_quarter_mrq',
        'Profit Margin': 'profit_margin',
        'Operating Margin (ttm)': 'operating_margin_ttm',
        'Return on Assets (ttm)': 'return_on_assets_ttm',
        'Return on Equity (ttm)': 'return_on_equity_ttm',
        'Revenue (ttm)': 'revenue_ttm',
        'Revenue Per Share (ttm)': 'revenue_per_share_ttm',
        'Quarterly Revenue Growth (yoy)': 'quarterly_revenue_growth_yoy',
        'Gross Profit (ttm)': 'gross_profit_ttm',
        'EBITDA': 'ebitda',
        'Net Income Avi to Common (ttm)': 'net_income_avi_to_common_ttm',
        'Diluted EPS (ttm)': 'diluted_eps_ttm',
        'Quarterly Earnings Growth (yoy)': 'quarterly_earnings_growth_yoy',
        'Total Cash (mrq)': 'total_cash_mrq',
        'Total Cash Per Share (mrq)': 'total_cash_per_share_mrq',
        'Total Debt (mrq)': 'total_debt_mrq',
        'Total Debt/Equity (mrq)': 'total_debt_to_equity_mrq',
        'Current Ratio (mrq)': 'current_ratio_mrq',
        'Book Value Per Share (mrq)': 'book_value_per_share_mrq',
        'Operating Cash Flow (ttm)': 'operating_cash_flow_ttm',
        'Levered Free Cash Flow (ttm)': 'levered_free_cash_flow_ttm',
        'Ticker': 'ticker'
        }

def get_ticker_data(ticker): http_headers = {"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9", "accept-encoding": "gzip, deflate, br", "accept-language": "en-GB,en;q=0.9,en-US;q=0.8,ml;q=0.7", "cache-control": "max-age=0", "dnt": "1", "sec-fetch-dest": "document", "sec-fetch-mode": "navigate", "sec-fetch-site": "none", "sec-fetch-user": "?1", "upgrade-insecure-requests": "1", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36"}

# GET & Parse Response from Summary URL to ticker_data dictionary
summary_url = "https://finance.yahoo.com/quote/%s?p=%s" % (ticker, ticker)
summary_response = requests.get(summary_url, verify=True, headers=http_headers, timeout=30)
summary_parser = html.fromstring(summary_response.text)
current_price = summary_parser.xpath('//*[@id="quote-header-info"]/div[3]/div[1]/div[1]/fin-streamer[1]/text()')
summary_table = summary_parser.xpath('//div[contains(@data-test,"summary-table")]//tr')
ticker_data = OrderedDict()
ticker_data.update({'Current Price': current_price[0]})
for table_data in summary_table:
    raw_table_key = table_data.xpath('.//td[1]//text()')
    raw_table_value = table_data.xpath('.//td[2]//text()')
    table_key = ''.join(raw_table_key).strip()
    table_value = ''.join(raw_table_value).strip()
    ticker_data.update({table_key: table_value})

# GET & Parse Response from Statistics URL to ticker_data dictionary
statistics_url = "https://finance.yahoo.com/quote/%s/key-statistics?p=%s" % (ticker, ticker)
statistics_response = requests.get(statistics_url, verify=True, headers=http_headers, timeout=30)
statistics_parser = html.fromstring(statistics_response.text)
statistics_table = statistics_parser.xpath('//div[contains(@class,"Mstart(a) Mend(a)")]//tr')
for table_data in statistics_table:
    raw_table_key = table_data.xpath('.//td[1]//text()')
    raw_table_value = table_data.xpath('.//td[2]//text()')
    table_key = ''.join(raw_table_key).strip()
    table_value = ''.join(raw_table_value).strip()
    ticker_data.update({table_key: table_value})

# Add Ticker key to ticker_data dictionary
ticker_data.update({'Ticker': ticker})

# Return TickerData object from ticker_data disctionary
return ticker_data_object(ticker_data)

if name == "main":

# Get ticker_data_translator dictionary
ticker_data_translator = get_ticker_data_translator()

stock_tickers_list = ['GOOGL', 'AMZN', 'AAPL', 'META', 'MSFT', 'NVDA', 'TSLA']

for stock_ticker in stock_tickers_list:
    ticker_data = get_ticker_data(stock_ticker)
    print(ticker_data.ticker + " current price: " + ticker_data.current_price)