|
| 1 | +#TODO - refactor & clean code |
1 | 2 | import csv
|
| 3 | +import time |
2 | 4 | from datetime import datetime
|
| 5 | +from datetime import date |
| 6 | +from selenium import webdriver |
| 7 | +from selenium.webdriver.support.ui import WebDriverWait |
| 8 | +from selenium.webdriver.support import expected_conditions as EC |
| 9 | +from selenium.webdriver.chrome.options import Options |
| 10 | +from selenium.webdriver.common.action_chains import ActionChains |
| 11 | +from selenium.webdriver.common.keys import Keys |
| 12 | +from selenium.webdriver.common.by import By |
3 | 13 |
|
4 |
| -import requests |
5 |
| -from bs4 import BeautifulSoup |
6 |
| - |
7 |
| -city = input("Enter City") |
8 |
| -url = "https://www.wunderground.com/weather/in/" + city |
9 |
| - |
10 |
| -try: |
11 |
| - response = requests.get(url) |
12 |
| -except requests.exceptions.RequestException as e: |
13 |
| - print(e) |
14 |
| - exit(1) |
15 |
| - |
16 |
| -try: |
17 |
| - response.raise_for_status() |
18 |
| -except Exception as e: |
19 |
| - print(e) |
20 |
| - exit(1) |
21 |
| - |
22 |
| -html = response.text |
23 |
| -soup = BeautifulSoup(html, "lxml") |
24 |
| -out2 = soup.find(class_="small-12 medium-4 large-3 columns forecast-wrap") |
25 |
| -out3 = out2.find(class_="columns small-12") |
26 |
| -out4 = soup.find(class_="data-module additional-conditions") |
27 |
| - |
28 |
| -Time = datetime.now().strftime("%H:%M") |
29 |
| -Date = out2.find("span", attrs={"class": "date"}).get_text() |
30 |
| -Temperature = out2.find("span", attrs={"class": "temp"}).get_text() |
31 |
| -Temperature = " ".join(Temperature.split()) |
32 |
| -Precipitation = ( |
33 |
| - "Precipitate:" + out3.find("a", attrs={"class": "hook"}).get_text().split(" ", 1)[0] |
| 14 | +#TODO - Add input checking |
| 15 | +city = input("City >") |
| 16 | +state = input("State >") |
| 17 | + |
| 18 | +url = 'https://www.wunderground.com' |
| 19 | + |
| 20 | +#Supresses warnings and specifies the webdriver to run w/o a GUI |
| 21 | +options = Options() |
| 22 | +options.headless = True |
| 23 | +options.add_argument('log-level=3') |
| 24 | +driver = webdriver.Chrome(options=options) |
| 25 | + |
| 26 | +driver.get(url) |
| 27 | +#----------------------------------------------------- |
| 28 | +# Connected successfully to the site |
| 29 | +#Passes the city and state input to the weather sites search box |
| 30 | + |
| 31 | +searchBox = driver.find_element(By.XPATH, '//*[@id="wuSearch"]') |
| 32 | +location = city + " " + state |
| 33 | + |
| 34 | +action = ActionChains(driver) |
| 35 | +searchBox.send_keys(location) |
| 36 | +element = WebDriverWait(driver, 10).until( |
| 37 | + EC.presence_of_element_located((By.XPATH, '//*[@id="wuForm"]/search-autocomplete/ul/li[2]/a/span[1]')) |
34 | 38 | )
|
35 |
| -other = out3.find("a", attrs={"class": "module-link"}).get_text().split(".") |
36 |
| -sky = other[0] |
37 |
| -Wind = other[2].strip() |
| 39 | +searchBox.send_keys(Keys.RETURN) |
| 40 | +#----------------------------------------------------- |
| 41 | +#Gather weather data |
| 42 | +#City - Time - Date - Temperature - Precipitation - Sky - Wind |
| 43 | + |
| 44 | +#waits till the page loads to begin gathering data |
| 45 | +precipitationElem = WebDriverWait(driver, 10).until( |
| 46 | + EC.presence_of_element_located((By.XPATH, '//*[@id="inner-content"]/div[3]/div[1]/div/div[3]/div/lib-city-today-forecast/div/div[1]/div/div/div/a[1]')) |
| 47 | +) |
| 48 | +precipitationElem = driver.find_element(By.XPATH, '//*[@id="inner-content"]/div[3]/div[1]/div/div[3]/div/lib-city-today-forecast/div/div[1]/div/div/div/a[1]') |
| 49 | +precip = "Precipitation:" + precipitationElem.text.split()[0] |
| 50 | + |
| 51 | +windAndSkyElem = driver.find_element(By.XPATH, '//*[@id="inner-content"]/div[3]/div[1]/div/div[3]/div/lib-city-today-forecast/div/div[1]/div/div/div/a[2]') |
| 52 | +description = windAndSkyElem.text.split(". ") |
| 53 | +sky = description[0] |
| 54 | +temp = description[1] |
| 55 | +wind = description[2] |
| 56 | + |
| 57 | +#Format the date and time |
| 58 | +time = datetime.now().strftime("%H:%M") |
| 59 | +today = date.today() |
| 60 | +date = today.strftime("%b-%d-%Y") |
| 61 | + |
| 62 | +print(city, time, date, temp, precip, sky, wind) |
38 | 63 |
|
39 | 64 | with open("weather.csv", "a") as new_file:
|
40 | 65 | csv_writer = csv.writer(new_file)
|
41 |
| - csv_writer.writerow([city, Time, Date, Temperature, Precipitation, sky, Wind]) |
| 66 | + csv_writer.writerow([city, time, date, temp, precip, sky, wind]) |
| 67 | + |
| 68 | +driver.close() |
0 commit comments