Created
March 8, 2018 18:08
-
-
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.
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
| 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