-1

Write code to get a list of tickers for all S&P 500 stocks from Wikipedia. As of 2/24/2021, there are 505 tickers in that list. You can use any method you want as long as the code actually queries the following website to get the list:

https://en.wikipedia.org/wiki/List_of_S%26P_500_companies

One way would be to use the requests module to get the HTML code and then use the re module to extract the tickers. Another option would be the .read_html function in pandas and then export the tickers column to a list.

You should save the tickers in a list with the name sp500_tickers

Emily
  • 11
  • 1
    Was this copy pasted out of homework? Make an attempt and if you run into errors, post specifics including relevant code and error messages. – schumacher574 Mar 15 '21 at 06:43
  • 1
    Pleaase explain your problem, and do not copy paste your homework. Please show a working piece of code you wrote yet and sample data about your trial to solve this problem, also give and copy the specific error messages or details you would like help about. So we can copy paste and reproduce, so it is easier to answer your question. See MRE here: https://stackoverflow.com/help/minimal-reproducible-example – Malo Mar 15 '21 at 15:56
  • Does this answer your question? [Get the S&P 500 tickers list](https://stackoverflow.com/questions/44232578/get-the-sp-500-tickers-list) – Asclepius Mar 26 '23 at 01:09

1 Answers1

2

This will grab the data in the table named 'constituents'.

# find a specific table by table count
import pandas as pd
import requests
from bs4 import BeautifulSoup

res = requests.get("https://en.wikipedia.org/wiki/List_of_S%26P_500_companies")
soup = BeautifulSoup(res.content,'lxml')
table = soup.find_all('table')[0] 
df = pd.read_html(str(table))
print(df[0].to_json(orient='records'))

Result:

[{"Symbol":"MMM","Security":"3M Company","SEC filings":"reports","GICS Sector":"Industrials","GICS Sub-Industry":"Industrial Conglomerates","Headquarters Location":"St. Paul, Minnesota","Date first added":"1976-08-09","CIK":66740,"Founded":"1902"},{"Symbol":"ABT","Security":"Abbott Laboratories","SEC filings":"reports","GICS Sector":"Health Care","GICS Sub-Industry":"Health Care Equipment","Headquarters Location":"North Chicago, Illinois","Date first added":"1964-03-31","CIK":1800,"Founded":"1888"},{"Symbol":"ABBV","Security":"AbbVie Inc.","SEC filings":"reports","GICS Sector":"Health Care","GICS Sub-Industry":"Pharmaceuticals","Headquarters Location":"North Chicago, Illinois","Date first added":"2012-12-31","CIK":1551152,"Founded":"2013 (1888)"},{"Symbol":"ABMD","Security":"Abiomed","SEC filings":"reports","GICS Sector":"Health Care","GICS Sub-Industry":"Health Care Equipment","Headquarters Location":"Danvers, Massachusetts","Date first added":"2018-05-31","CIK":815094,"Founded":"1981"},{"Symbol":"ACN","Security":"Accenture","SEC filings":"reports","GICS Sector":"Information Technology","GICS Sub-Industry":"IT Consulting & Other Services","Headquarters Location":"Dublin, Ireland","Date first added":"2011-07-06","CIK":1467373,"Founded":"1989"},{"Symbol":"ATVI","Security":"Activision Blizzard","SEC filings":"reports","GICS Sector":"Communication Services","GICS Sub-Industry":"Interactive Home Entertainment","Headquarters Location":"Santa Monica, California","Date first added":"2015-08-31","CIK":718877,"Founded":"2008"},{"Symbol":"ADBE","Security":"Adobe Inc.","SEC filings":"reports","GICS Sector":"Information Technology","GICS Sub-Industry":"Application Software","Headquarters Location":"San Jose, California","Date first added":"1997-05-05","CIK":796343,"Founded":"1982"},
Etc., etc., etc.

That's JSON. If you want a table, kind of like what you would use in Excel, simply print the df.

Result:

[    Symbol             Security SEC filings             GICS Sector  \
 0      MMM           3M Company     reports             Industrials   
 1      ABT  Abbott Laboratories     reports             Health Care   
 2     ABBV          AbbVie Inc.     reports             Health Care   
 3     ABMD              Abiomed     reports             Health Care   
 4      ACN            Accenture     reports  Information Technology   
 ..     ...                  ...         ...                     ...   
 500    YUM      Yum! Brands Inc     reports  Consumer Discretionary   
 501   ZBRA   Zebra Technologies     reports  Information Technology   
 502    ZBH        Zimmer Biomet     reports             Health Care   
 503   ZION        Zions Bancorp     reports              Financials   
 504    ZTS               Zoetis     reports             Health Care   
 
                       GICS Sub-Industry    Headquarters Location  \
 0              Industrial Conglomerates      St. Paul, Minnesota   
 1                 Health Care Equipment  North Chicago, Illinois   
 2                       Pharmaceuticals  North Chicago, Illinois   
 3                 Health Care Equipment   Danvers, Massachusetts   
 4        IT Consulting & Other Services          Dublin, Ireland   
 ..                                  ...                      ...   
 500                         Restaurants     Louisville, Kentucky   
 501  Electronic Equipment & Instruments   Lincolnshire, Illinois   
 502               Health Care Equipment          Warsaw, Indiana   
 503                      Regional Banks     Salt Lake City, Utah   
 504                     Pharmaceuticals   Parsippany, New Jersey   
 
     Date first added      CIK      Founded  
 0         1976-08-09    66740         1902  
 1         1964-03-31     1800         1888  
 2         2012-12-31  1551152  2013 (1888)  
 3         2018-05-31   815094         1981  
 4         2011-07-06  1467373         1989  
 ..               ...      ...          ...  
 500       1997-10-06  1041061         1997  
 501       2019-12-23   877212         1969  
 502       2001-08-07  1136869         1927  
 503       2001-06-22   109380         1873  
 504       2013-06-21  1555280         1952  
 
 [505 rows x 9 columns]]

Alternatively, you can export the df to a CSV file.

df.to_csv('constituents.csv')
ASH
  • 20,759
  • 19
  • 87
  • 200