Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
76 lines (64 sloc) 2.06 KB
"""
File: stock_scraper.py
Author: Zachary King
Description: A web scraper that retrieves the
stock value for a given list of companies
(by symbols), and plots the values on a
bar graph accordingly, with color-coding.
"""
import requests
import time
from bs4 import BeautifulSoup
import numpy as np
import matplotlib.pyplot as plt
start_time = time.time()
# Variable declarations
symbols = ["aapl", "goog", "msft", "yhoo", "amzn", "fb", "twtr", "nflx"]
numCompanies = len(symbols)
values = []
arrows = []
# Scrape the data
for s in symbols:
url = "http://finance.yahoo.com/q?&s=" + s
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
price = soup.find_all("span", {"data-reactid": "36"})
arrow = soup.find_all("span", {"data-reactid": "37"})
if arrow[0].text[0] == '-':
arrows.append(u'neg_arrow')
else:
arrow.append(u'pos_arrow')
print(s.upper() + " share value: $" + price[0].text)
values.append(price[0].text)
symbols[symbols.index(s)] = symbols[symbols.index(s)].upper()
# convert values from strings to floats
for i in range(numCompanies):
values[i] = float(values[i])
# Plot and figure
fig = plt.figure()
fig.canvas.set_window_title('Stock-Scraper')
ax = fig.add_subplot(111)
index = np.arange(numCompanies)
rects = ax.bar(index, values, 0.25, alpha=0.5)
# Show the number value for the share above the bar
for i in range(numCompanies):
ax.annotate('$' + str(values[i]), xy=(i+0.12, values[i]), xytext=(i - 0.12, values[i] + 10),
arrowprops=dict(arrowstyle='-'),
)
# Labels and titles on graph
plt.xlabel('Company')
plt.ylabel('Share Value')
plt.title('Current Share Value for Companies')
plt.xticks(index + 0.25, symbols)
# Color the bars based on the companies' stock status
for i, arrow in enumerate(arrows):
if (arrow == [u'pos_arrow']):
rects[i].set_color('g') # stock is going up
elif (arrow == [u'neg_arrow']):
rects[i].set_color('r') # stock is going down
else:
rects[i].set_color('b') # unknown data
print("--- %s seconds ---" % (time.time() - start_time))
# Show the graph
fig.tight_layout()
plt.show()
You can’t perform that action at this time.