File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 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 ()
Original file line number Diff line number Diff line change 31311 . ** 29_json_to_yaml.py** : Convert JSON to YAML
32321 . ** 30_fullcontact.py** : Call the [ FullcContact] ( https://www.fullcontact.com/developer/ ) API
33331 . ** 31_youtube_sentiment.py** : Calculate sentiment score from the comments of a Youtube video
34+ 1 . ** 32_stock_scraper.py** : Get stock prices
You can’t perform that action at this time.
0 commit comments