From 7591683535eca836d8d86974635e9c7979ab7abd Mon Sep 17 00:00:00 2001 From: Michael Herman Date: Mon, 1 Feb 2016 22:33:14 -0700 Subject: [PATCH] added stock prices script --- 32_stock_scraper.py | 38 ++++++++++++++++++++++++++++++++++++++ readme.md | 1 + 2 files changed, 39 insertions(+) create mode 100644 32_stock_scraper.py diff --git a/32_stock_scraper.py b/32_stock_scraper.py new file mode 100644 index 0000000..3bc2e7d --- /dev/null +++ b/32_stock_scraper.py @@ -0,0 +1,38 @@ +import urllib.request +from bs4 import BeautifulSoup + + +def get_stock_tickers(): + req = urllib.request.Request( + 'http://en.wikipedia.org/wiki/List_of_S%26P_500_companies') + page = urllib.request.urlopen(req) + soup = BeautifulSoup(page, 'html.parser') + table = soup.find('table', {'class': 'wikitable sortable'}) + tickers = [] + for row in table.findAll('tr'): + col = row.findAll('td') + if len(col) > 0: + tickers.append(str(col[0].string.strip())) + tickers.sort() + return tickers + + +def get_stock_prices(ticker_list): + for ticker in ticker_list: + htmlfile = urllib.request.urlopen( + "http://finance.yahoo.com/q?s={0}".format(ticker) + ) + htmltext = htmlfile.read() + soup = BeautifulSoup(htmltext, 'html.parser') + htmlSelector = 'yfs_l84_{0}'.format(ticker.lower()) + for price in soup.find_all(id=htmlSelector): + print('{0} is {1}'.format(ticker, price.text)) + + +def main(): + all_tickers = get_stock_tickers() + get_stock_prices(all_tickers) + + +if __name__ == '__main__': + main() diff --git a/readme.md b/readme.md index 363d345..926be9d 100644 --- a/readme.md +++ b/readme.md @@ -31,3 +31,4 @@ 1. **29_json_to_yaml.py**: Convert JSON to YAML 1. **30_fullcontact.py**: Call the [FullcContact](https://www.fullcontact.com/developer/) API 1. **31_youtube_sentiment.py**: Calculate sentiment score from the comments of a Youtube video +1. **32_stock_scraper.py**: Get stock prices