def get_sap_symbols(name='sap500'):
"""Get ticker symbols constituting S&P
Args:
name(str): should be 'sap500' or 'sap100'
"""
if name == 'sap500':
site = 'http://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
elif name == 'sap100':
site = 'https://en.wikipedia.org/wiki/S%26P_100'
else:
raise NameError('invalid input: name should be "sap500" or "sap100"')
# fetch data from yahoo finance
page = urlopen(site)
soup = BeautifulSoup(page, 'html.parser')
table = soup.find('table', {'class': 'wikitable sortable'})
symbols = []
for row in table.findAll('tr'):
col = row.findAll('td')
if len(col) > 0:
symbol = col[0].string.replace('.', '-')
symbols.append(str(symbol))
return symbols
评论列表
文章目录