Search code examples
pythonyfinance

Request: Help on HTTPError: 404 Client Error: Not Found for url


I have tickers stored in a list, but need the ISIN codes. Using yfinance.isin you are able to get the ISIN codes, so I wrote a for loop that is going through the list to get the isin codes.

However, some tickers don't exist on yfinance so I need to drop/store these somewhere and tweak to code so it proceeds.

tickers = list_companies
tickers = [yf.Ticker(ticker) for ticker in tickers]
dfs = []

for ticker in tickers:
    isin = ticker.isin

The error I am getting is:

---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
<ipython-input-40-66c3d6fb28b7> in <module>
      2 
      3 for ticker in tickers:
----> 4     isin = ticker.isin

~/opt/anaconda3/lib/python3.8/site-packages/yfinance/ticker.py in isin(self)
    100     @property
    101     def isin(self):
--> 102         return self.get_isin()
    103 
    104     @property

~/opt/anaconda3/lib/python3.8/site-packages/yfinance/base.py in get_isin(self, proxy)
   1404 
   1405         self._quote.proxy = proxy
-> 1406         if self._quote.info is None:
   1407             # Don't print error message cause self._quote.info will print one
   1408             return None

~/opt/anaconda3/lib/python3.8/site-packages/yfinance/scrapers/quote.py in info(self)
    570         if self._info is None:
    571             # self._scrape(self.proxy)  # decrypt broken
--> 572             self._fetch(self.proxy)
    573 
    574             self._fetch_complementary(self.proxy)

~/opt/anaconda3/lib/python3.8/site-packages/yfinance/scrapers/quote.py in _fetch(self, proxy)
    721         modules = ['summaryProfile', 'financialData', 'quoteType',
    722                      'defaultKeyStatistics', 'assetProfile', 'summaryDetail']
--> 723         result = self._data.get_raw_json(
    724             _BASIC_URL_ + f"/{self._data.ticker}", params={"modules": ",".join(modules), "ssl": "true"}, proxy=proxy
    725         )

~/opt/anaconda3/lib/python3.8/site-packages/yfinance/data.py in get_raw_json(self, url, user_agent_headers, params, proxy, timeout)
    212     def get_raw_json(self, url, user_agent_headers=None, params=None, proxy=None, timeout=30):
    213         response = self.get(url, user_agent_headers=user_agent_headers, params=params, proxy=proxy, timeout=timeout)
--> 214         response.raise_for_status()
    215         return response.json()
    216 

~/opt/anaconda3/lib/python3.8/site-packages/requests/models.py in raise_for_status(self)
   1019 
   1020         if http_error_msg:
-> 1021             raise HTTPError(http_error_msg, response=self)
   1022 
   1023     def close(self):

HTTPError: 404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/AGN?modules=summaryProfile%2CfinancialData%2CquoteType%2CdefaultKeyStatistics%2CassetProfile%2CsummaryDetail&ssl=true

New code:

import requests
df = []
company_na = []
for ticker in tickers:
    try: 
        isin = ticker.isin
        data = isin
        #data['Ticker'] = ticker.ticker
        df.append(data)
    except requests.exceptions.HTTPError:
        print('error ticker not found:')
        company_na.append(ticker)
        continue

df = pd.DataFrame(df, columns = ['ISIN'])
dfs['Ticker'] = ticker.ticker
print(dfs)

Solution

  • Don't use a list comprehension. Use a plain for loop so you can put a try/except block around each request.

    import requests
    
    for ticker in tickers:
        try:
            isin = ticker.isin
            # more code to handle the isin result
        except requests.exceptions.HTTPError:
            # handle however you like