Blogs

Download All S&P 500 Stocks with yFinance and BeautifulSoup

Get all s&p 500 listed stock symbols from Wikipedia using BeautifulSoup. Then download all symbols using yfinance. 

import bs4 as bs
import requests
import yfinance as yf
import datetime

resp = requests.get('http://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
soup = bs.BeautifulSoup(resp.text, 'lxml')
table = soup.find('table', {'class': 'wikitable sortable'})
tickers = []
for row in table.findAll('tr')[1:]:
    ticker = row.findAll('td')[0].text
    tickers.append(ticker)

tickers = [s.replace('\n', '') for s in tickers]
start = datetime.datetime(2016,5,10)
end = datetime.datetime(2021,5,10)
data = yf.download(tickers, start=start, end=end)
data = data['Adj Close']