I tried to take the S&P 500 list from wiki. In it, I want to remove some unwanted stocks. My code successfully removed any of the stock in the list EXCEPT the stock 'FOX'.
It is a working code. I just can not remove the stock 'FOX'. Can anyone help me?
import bs4 as bs
import datetime as dt
import os
import pandas as pd
import pandas_datareader.data as web
import pickle
import requests
# Save S&P 500 to pickle (Source: Wikipedia)
def save_sp500_tickers():
resp = requests.get('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
soup = bs.BeautifulSoup(resp.text, 'lxml')
table = soup.find('table', {'class': 'wikitable sortable'})
tickers = []
bad_chars = ['\n']
for row in table.findAll('tr')[1:]:
ticker = row.findAll('td')[0].text
tickers.append(ticker)
tickers = [s.replace('\n', '') for s in tickers] # Remove '\n' from the string
tickers = [s.replace('.', '-') for s in tickers] # Replace '.' by '-'
remove_list = ['CTVA', 'DOW', 'FOXA', 'FOX']
[tickers.remove(s) for s in tickers if s in remove_list]
print(tickers)
return tickers
save_sp500_tickers()