Created
September 11, 2024 14:41
-
-
Save skipperkongen/92fde5e61683e7892926da8836dfe78a to your computer and use it in GitHub Desktop.
Get S&P 500 stocks as dataframe from Wikipedia
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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