Skip to content

Instantly share code, notes, and snippets.

@AO8
Last active January 16, 2018 23:00
Show Gist options
  • Select an option

  • Save AO8/778cf8ad211f7aefe80a6f6899720b8d to your computer and use it in GitHub Desktop.

Select an option

Save AO8/778cf8ad211f7aefe80a6f6899720b8d to your computer and use it in GitHub Desktop.
Another simple Dow Jones tracker that scrapes a website and writes data to a CSV file using Python 3 and Beautiful Soup.
import urllib.request
import csv
import re
from bs4 import BeautifulSoup
from datetime import datetime as dt
dow_jones_page = "https://www.bloomberg.com/quote/INDU:IND"
html = urllib.request.urlopen(dow_jones_page)
soup = BeautifulSoup(html, "html.parser")
price_div = soup.find("div", attrs={"class":"price"}) # use Chrome Developer Tools to inspect page
price = price_div.text # returns a string
price = re.sub(",", "", price) # removes comma for float conversion, if needed down the road
day_recorded = dt.now().strftime("%x") # see strftime.org/ for more options
time_recorded = dt.now().strftime("%H:%M %p")
with open("dow_tracker.csv", "a") as f:
writer = csv.writer(f)
writer.writerow([day_recorded, time_recorded, price])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment