-
Notifications
You must be signed in to change notification settings - Fork 9
/
core.py
44 lines (31 loc) · 989 Bytes
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding: utf-8 -*-
'''
core
----
core objects and functions
'''
###############################################################################
from __future__ import (
absolute_import,
division,
print_function,
unicode_literals,
)
from requests import get
from bs4 import BeautifulSoup
def get_sp500_list(symbols_only=True):
'''
returns a list of S&P500 stocks from wikipedia
:rtype: `list`
:param symbols_only: if True, fcn returns a list of symbols, else dicts
:type symbols_only: `bool`
'''
result = list()
url = 'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
page = get(url)
soup = BeautifulSoup(page.content, 'html.parser')
rows = soup.find('table').findAll('tr')
keys = [k.text for k in rows[0].findAll('th')]
for r in rows[1:]:
result.append(dict(zip(keys, [f.text for f in r.findAll('td')])))
return result if not symbols_only else [r['Ticker symbol'] for r in result]