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‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
importurllib.request
2+
frombs4importBeautifulSoup
3+
4+
5+
defget_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+
forrowintable.findAll('tr'):
13+
col=row.findAll('td')
14+
iflen(col) >0:
15+
tickers.append(str(col[0].string.strip()))
16+
tickers.sort()
17+
returntickers
18+
19+
20+
defget_stock_prices(ticker_list):
21+
fortickerinticker_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+
forpriceinsoup.find_all(id=htmlSelector):
29+
print('{0} is{1}'.format(ticker, price.text))
30+
31+
32+
defmain():
33+
all_tickers=get_stock_tickers()
34+
get_stock_prices(all_tickers)
35+
36+
37+
if__name__=='__main__':
38+
main()

‎readme.md‎

Lines changed: 1 addition & 0 deletions
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)