Skip to content

Instantly share code, notes, and snippets.

@skipperkongen
Created September 11, 2024 14:41
Show Gist options
  • Save skipperkongen/92fde5e61683e7892926da8836dfe78a to your computer and use it in GitHub Desktop.
Save skipperkongen/92fde5e61683e7892926da8836dfe78a to your computer and use it in GitHub Desktop.
Get S&P 500 stocks as dataframe from Wikipedia
import requests
import pandas as pd
from bs4 import BeautifulSoup
# Fetching S&P 500 tickers and company names from Wikipedia
url = 'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table', {'class': 'wikitable sortable'})
tickers = []
company_names = []
# Loop through the table rows and extract ticker symbols and company names
for row in table.findAll('tr')[1:]:
ticker = row.findAll('td')[0].text.strip()
company_name = row.findAll('td')[1].text.strip()
tickers.append(ticker)
company_names.append(company_name)
# Create a DataFrame for better organization and display
df = pd.DataFrame({
'Symbol': tickers,
'Company Name': company_names
})
# Display the DataFrame
df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment