Skip to content
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
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 = "" + 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] == '-':
print(s.upper() + " share value: $" + 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()
ax = fig.add_subplot(111)
index = np.arange(numCompanies)
rects =, 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),
# Labels and titles on graph
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
rects[i].set_color('b') # unknown data
print("--- %s seconds ---" % (time.time() - start_time))
# Show the graph
You can’t perform that action at this time.