Skip to content

Commit cae7fa8

Browse files
committed
Fixed outdated/broken application, new update implements Selenium to acheive a working solution.
1 parent ef1fecc commit cae7fa8

File tree

2 files changed

+64
-34
lines changed

2 files changed

+64
-34
lines changed

Weather Scrapper/weather.csv

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ Manhatten,07:02,Tue 06/05,Low 71 F,Precipitate:30%,Isolated Thunderstorms,Winds
33
Manhattan,07:03,Wed 06/06,Low 47 F,Precipitate:10%,Partly Cloudy,Winds NE at 10 to 15 mph
44
Aligarh,07:03,Wed 06/06,High 109 F,Precipitate:20%,Partly Cloudy,Winds ENE at 10 to 15 mph
55
Delhi,07:03,Wed 06/06,High 107 F,Precipitate:40%,AM Thunderstorms,Winds E at 10 to 15 mph
6+
Portland,02:20,Aug-21-2022,Low 61F,Precipitation:3%,A few clouds,Winds light and variable.
7+
Washington,02:21,Aug-21-2022,High 83F,Precipitation:48%,Cloudy early with scattered thunderstorms developing this afternoon,Winds SSE at 5 to 10 mph
8+
Guadalajara,02:21,Aug-21-2022,Scattered thunderstorms developing this afternoon,Precipitation:52%,Mostly cloudy this morning,High near 80F

Weather Scrapper/weather.py

+61-34
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,68 @@
1+
#TODO - refactor & clean code
12
import csv
3+
import time
24
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
313

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]'))
3438
)
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)
3863

3964
with open("weather.csv", "a") as new_file:
4065
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

Comments
 (0)