Using QQQ ETF
To obtain an official list of Nasdaq 100 symbols as constituents of the QQQ ETF:
def list_qqq_holdings() -> pd.DataFrame:
# Ref: https://stackoverflow.com/a/75846060/
# Source: https://www.invesco.com/us/financial-products/etfs/holdings?ticker=QQQ
converters = {'Holding Ticker': lambda s: s.rstrip(' ')}
return pd.read_csv(url, converters=converters, index_col='Holding Ticker')
Using Wikipedia
To obtain an unofficial list of Nasdaq 100 symbols, pandas.read_html
can be used. A parser such as lxml
or bs4
+html5lib
is also required as it is used internally by pandas
.
Note that the list on Wikipedia can be outdated.
import pandas as pd
def list_wikipedia_nasdaq100() -> pd.DataFrame:
# Ref: https://stackoverflow.com/a/75846060/
url = 'https://en.m.wikipedia.org/wiki/Nasdaq-100'
return pd.read_html(url, attrs={'id': "constituents"}, index_col='Ticker')[0]
>> df = list_wikipedia_nasdaq100()
>> df.head()
Company ... GICS Sub-Industry
Ticker ...
ATVI Activision Blizzard ... Interactive Home Entertainment
ADBE Adobe Inc. ... Application Software
ADP ADP ... Data Processing & Outsourced Services
ABNB Airbnb ... Internet & Direct Marketing Retail
ALGN Align Technology ... Health Care Supplies
[5 rows x 3 columns]
>> symbols = df.index.to_list()
>> symbols[:5]
['ATVI', 'ADBE', 'ADP', 'ABNB', 'ALGN']
Using Slickcharts
import pandas as pd
import requests
def list_slickcharts_nasdaq100() -> pd.DataFrame:
# Ref: https://stackoverflow.com/a/75846060/
url = 'https://www.slickcharts.com/nasdaq100'
user_agent = 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0' # Default user-agent fails.
response = requests.get(url, headers={'User-Agent': user_agent})
return pd.read_html(response.text, match='Symbol', index_col='Symbol')[0]
These were tested with Pandas 1.5.3.
The results can be cached for a certain period of time, e.g. 8 hours, in memory and/or on disk, so as to avoid the risk of excessive repeated calls to the source.
A similar answer for the S&P 500 is here.