Skip to content

Instantly share code, notes, and snippets.

@AO8
Created March 8, 2018 18:08
Show Gist options
  • Select an option

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

Select an option

Save AO8/db3856e3c4c39007c9c2dfe4dcd170c7 to your computer and use it in GitHub Desktop.
Dow Jones tracker re-written with Requests package. Scrape and write data to a CSV file using Python 3 and Beautiful Soup.
import requests
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 = requests.get(dow_jones_page).text
soup = BeautifulSoup(html, "html.parser")
price_div = soup.select("div[class=price]") # returns list
price = price_div[0].text
price = re.sub(",", "", price) # removes comma
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", newline="") as f:
writer = csv.writer(f)
writer.writerow([day_recorded, time_recorded, price])
# 3/8/2018 10:03 AM 24766.22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment