Made using the tutorial at: https://www.dataquest.io/blog/web-scraping-tutorial-python/
It reads the zip code / lat-long conversion information at: https://gist.github.com/erichurst/7882666 So you'll need to go there, click to view the full file, then copy and paste that into a text file called, “ZipsLatLong.txt”. That file has to be in the same folder as the Python file to properly work.
Weather data comes from: https://forecast.weather.gov
Enjoy!
# Modified from the tutorial at: # https://www.dataquest.io/blog/web-scraping-tutorial-python/ # # Zip Code to Lat-Long conversion done with this file: # https://gist.github.com/erichurst/7882666 # # Weather data comes from: # https://forecast.weather.gov import requests from bs4 import BeautifulSoup import csv userLocation = 0 userInput = 0 # Load data from ZipsLatLong.txt file csvReaderFile = open("ZipsLatLong.txt", 'r') csvReader = csv.reader(csvReaderFile) exampleData = list(csvReader) while userInput != "": userInput = input("Enter your zipcode for tonight's forecast: ") try: userZip = int(userInput) except: break for row in exampleData: for item in row: if str(item) == str(userZip): userLocation = row if userLocation == 0: print("That zip code is not in the list") else: lat = userLocation[1] longPlusSpace = userLocation[2] long = longPlusSpace[1:] weatherSite = "https://forecast.weather.gov/MapClick.php?lat=" + lat + "&lon=" + long page = requests.get(weatherSite) print() soup = BeautifulSoup(page.content, 'html.parser') seven_day = soup.find(id="seven-day-forecast") forecast_items = seven_day.find_all(class_="tombstone-container") tonight = forecast_items[0] period = tonight.find(class_="period-name").get_text() short_desc = tonight.find(class_="short-desc").get_text() try: temp = tonight.find(class_="temp").get_text() except: temp = "The weather service doesn't have a temperature for this zip code. Sorry!" print("\n\nYour Local Weather Forecast") print(period) print(short_desc) print(temp, "\n\n") csvReaderFile.close()