added stock prices script

This commit is contained in:
Michael Herman 2016-02-01 22:33:14 -07:00
parent 7a33539c8c
commit 7591683535
2 changed files with 39 additions and 0 deletions

38
32_stock_scraper.py Normal file
View File

@ -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()

View File

@ -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