Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EM5813/ff63bebad6567887233c94e305508bd6 to your computer and use it in GitHub Desktop.
Save EM5813/ff63bebad6567887233c94e305508bd6 to your computer and use it in GitHub Desktop.
Python code to get price data for all S&P500 tickers
# Import packages
import yfinance as yf
import pandas as pd
# Read and print the stock tickers that make up S&P500
tickers = pd.read_html(
'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0]
print(tickers.head())
# Get the data for this tickers from yahoo finance
data = yf.download(tickers.Symbol.to_list(),'2021-1-1','2021-4-28', auto_adjust=True)['Close']
print(data.head())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment