I use the Yahoo API a lot to get stock data from multiple exchanges in the world. The code normally works, but it look likes that there has been an update in the API. In the past when a stock was delisted, the except function in Python got the error and continued. The strange thing is that this error is caused by only some stocks, which are delisted?
For instance stock ´ANTM´, this stock is delisted. I receive this error while trying to scrape the stock data: AttributeError: 'float' object has no attribute 'upper'. This code runs till infinity.
!pip install yfinance
import yfinance as yf
import datetime as dt
start = '2021-01-01'
end = dt.datetime.today().strftime('%Y-%m-%d')
tickers=['AAPL', 'ANTM']
for ticker in tickers:
try:
df=yf.download(ticker, start, end, progress=False)
df.index = df.index.strftime('%Y/%m/%d')
df.to_csv(f'{ticker}.csv')
except Exception:
if ticker not in tickers:
continue
I have created a workaround, but this is slow for 1K of stocks.
#code with workaround
tickers=['AAPL','ANTM']
for ticker in tickers:
try:
if yf.Ticker(ticker).info['regularMarketPrice']!=None: #workaround
df=yf.download(ticker, start, end, progress=False)
df.index = df.index.strftime('%Y/%m/%d')
df.to_csv(f'{ticker}.csv')
else: #workaround
continue #workaround
except Exception:
if ticker not in tickers:
continue
What can I do to make sure that the code is catching the error, and continues with the code, which doesn't have a impact on the speed of the code?
It looks like that the API is updated, the except exception is working again.