I am trying to scrape stock market information from all the stocks concerning the FTSE250. I use Yahoo_Fin in order to do so. The code works, but I get an error that a stock is delisted. Hence, I tried to put-in an except exception code. I read the documentation about the try and except libraries, but could not find the correct answer. I don't receive a syntax error, but the except exception code doesn't do anything.
EDIT: Putting two except exceptions works, below is the updated code.
index_df = pdr.get_data_yahoo(index_name, start_date, end_date, progress=False)
index_df['Percent Change'] = index_df['Adj Close'].pct_change()
index_return = (index_df['Percent Change'] + 1).cumprod()[-1]
for ticker in tickers:
# Download historical data as CSV for each stock (makes the process faster)
try:
df = pdr.get_data_yahoo(ticker, start_date, end_date,progress=False)
df.to_csv(f'{ticker}.csv')
except:
except Exception:
if ticker not in tickers:
next(ticker)
for ticker in tickers:
try:
# Calculating returns relative to the market (returns multiple)
df['Percent Change'] = df['Adj Close'].pct_change()
stock_return = (df['Percent Change'] + 1).cumprod()[-1]
returns_multiple = round((stock_return / index_return), 2)
returns_multiples.extend([returns_multiple])
except Exception:
if ticker not in tickers:
next(ticker)