import urllib.request from re import findall import datetime import csv import os import time #################### # Gets the current directory and changes it to the desktop # REPLACE Kdog93 with YOUR USERNAME #################### print("My current Directory is at\n", os.getcwd()) os.chdir('C:\\Users\\Kdog93\\Desktop') class weather: def __init__(self): gustavo = True start = datetime.datetime.now() two_days = datetime.timedelta(days=2) #Naming the file todays date using the datetime module dateStr = str(start.month) + "." + str(start.day) + "." + str(start.year) self.fName = "weather" + dateStr + ".csv" self.f = open(self.fName, "a", newline="") while gustavo: self.now = datetime.datetime.now() self.scrape() self.writeToFile() self.sleep() if (self.now - start) >= two_days: self.f.close() print("Done!") gustavo = False def sleep(self): #seconds as a parameter #10 minute delay time.sleep(600) def scrape(self): url = "http://www.accuweather.com/en/us/atlanta-ga/30303/weather-forecast/348181" response = urllib.request.urlopen(url) #response if webpage is found ex.404 not found html = response.read() #Array of bytes htmlStr = html.decode() #Converts to string #Webscraping with three different capture groups #(weather condition, actual temperature, feels like temperature) data = findall('
([A-Za-z &0-9]+) ([0-9]+)°
\n?\t?\s*RealFeel® ([0-9]+)', htmlStr) for atup in data: newtup = atup + (str(self.now.time()),) self.newtup = newtup def writeToFile(self): #Appends to the file the weather data at that time csvWriter = csv.writer(self.f) csvWriter.writerow(list(self.newtup)) self.f.flush() #Make sure that you can watch the progress... pstr ="Writing to file: " + self.fName + "\n\t" print(pstr, self.newtup) weather()