Skip to content

Commit 7591683

Browse files
committed
added stock prices script
1 parent 7a33539 commit 7591683

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

32_stock_scraper.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import urllib.request
2+
from bs4 import BeautifulSoup
3+
4+
5+
def get_stock_tickers():
6+
req = urllib.request.Request(
7+
'http://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
8+
page = urllib.request.urlopen(req)
9+
soup = BeautifulSoup(page, 'html.parser')
10+
table = soup.find('table', {'class': 'wikitable sortable'})
11+
tickers = []
12+
for row in table.findAll('tr'):
13+
col = row.findAll('td')
14+
if len(col) > 0:
15+
tickers.append(str(col[0].string.strip()))
16+
tickers.sort()
17+
return tickers
18+
19+
20+
def get_stock_prices(ticker_list):
21+
for ticker in ticker_list:
22+
htmlfile = urllib.request.urlopen(
23+
"http://finance.yahoo.com/q?s={0}".format(ticker)
24+
)
25+
htmltext = htmlfile.read()
26+
soup = BeautifulSoup(htmltext, 'html.parser')
27+
htmlSelector = 'yfs_l84_{0}'.format(ticker.lower())
28+
for price in soup.find_all(id=htmlSelector):
29+
print('{0} is {1}'.format(ticker, price.text))
30+
31+
32+
def main():
33+
all_tickers = get_stock_tickers()
34+
get_stock_prices(all_tickers)
35+
36+
37+
if __name__ == '__main__':
38+
main()

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@
3131
1. **29_json_to_yaml.py**: Convert JSON to YAML
3232
1. **30_fullcontact.py**: Call the [FullcContact](https://www.fullcontact.com/developer/) API
3333
1. **31_youtube_sentiment.py**: Calculate sentiment score from the comments of a Youtube video
34+
1. **32_stock_scraper.py**: Get stock prices

0 commit comments

Comments
 (0)