Last active
June 21, 2018 19:14
-
-
Save AO8/bde37fbfa860042f0550ec9ca844ee06 to your computer and use it in GitHub Desktop.
Use Python, Requests, BeautifulSoup, CSV Writer, and MatPlotLib to scrape, write, and visualize a day on the stock market.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # From Python's Standard Library | |
| from time import sleep | |
| import datetime | |
| import csv | |
| # Third-party | |
| import requests | |
| import bs4 | |
| import matplotlib.pyplot as plt | |
| times = [] | |
| prices = [] | |
| # Gets Dow Jones Industrial Average every 30 seconds for 6.5 hours, 780 total data points | |
| for i in range(780): | |
| try: | |
| dj_page = "https://www.bloomberg.com/quote/INDU:IND" | |
| html = requests.get(dj_page).text | |
| soup = bs4.BeautifulSoup(html, "html.parser") | |
| price_div = soup.find("div", class_="price").text | |
| price = price_div.replace(",", "") # remove comma for float conversion in ln 29 | |
| date_time = datetime.datetime.now() | |
| # Write datetime and price to CSV file for future reference | |
| with open("dj.csv", "a", newline="") as f: | |
| writer = csv.writer(f) | |
| writer.writerow([date_time, price]) | |
| times.append(date_time) | |
| prices.append(float(price)) | |
| print(f"{i}: Dow at {price} on {date_time}") # fstring literals require Python 3.6+ | |
| sleep(30) | |
| except: | |
| continue | |
| # Visualize data in times list (x axis) and prices list (y axis) | |
| plt.plot(times,prices) | |
| plt.gcf().autofmt_xdate() | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment