Getting Weather data for All locations in US in python

857 views Asked by At

Hello I have a code but it gives only weather data for one location how to loop it for all locations in USA. Below is my code. I have written two locations. Is it possible to read from file and get the data of all locations in USA.


import urllib.parse
import urllib.request
import json
import time
import matplotlib.pyplot as plt
import pandas as pd
import os
from datetime import datetime,timedelta

def getWeatherForecast():
   now = datetime.now()
   startdate = (datetime.now() - timedelta(2)).strftime('%Y-%m-%d')
   enddate = (datetime.now() - timedelta(1)).strftime('%Y-%m-%d')
   LOCATION = ["Texas","California"]
   for i in LOCATION:
       requestUrl = "https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/"+i+"/"+startdate+"/"+enddate+"?unitGroup=metric&include=days&key=XXXXXXXXXXXXXXXXXXX&contentType=json"
       print('Weather requestUrl={requestUrl}'.format(requestUrl=requestUrl))
       req = urllib.request.urlopen(requestUrl)
       rawForecastData = req.read()
       req.close()
       d = json.loads(rawForecastData)
   return d

def weather():
   weatherForecast = getWeatherForecast()
   print('Weather forecast for {location}'.format(location=weatherForecast['resolvedAddress']))
   days = weatherForecast['days'];
   for day in days:
       print('{datetime} tempmax:{tempmax} tempmin:{tempmin} description:{description}'.format(datetime=day['datetime'],
                                                                                               tempmax=day["tempmax"],
                                                                                               tempmin=day["tempmin"],
                                                                                               description=day[
                                                                                                   "description"]))
       df = pd.DataFrame(days)
1

There are 1 answers

0
uozcan12 On

I found the all location in the USA and converted to a list. Maybe you can use like this. I put all requests into try/except method and time sleep 5 seconds to prevent error. Maybe you should read the all documentations about this API. I've just found these : https://www.visualcrossing.com/resources/documentation/weather-api/timeline-weather-api/

def getWeatherForecast():
   now = datetime.now()
   startdate = (datetime.now() - timedelta(2)).strftime('%Y-%m-%d')
   enddate = (datetime.now() - timedelta(1)).strftime('%Y-%m-%d')
   LOCATION =  ["Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut",
     "Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa",
     "Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan",
     "Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire",
     "New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio",
     "Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina",
     "South Dakota","Tennessee","Texas","Utah","Vermont","Virginia",
     "Washington","West Virginia","Wisconsin","Wyoming"]
   try:
       for i in LOCATION:
           requestUrl = "https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/"+i+"/"+startdate+"/"+enddate+"?unitGroup=metric&include=days&key=XXXXXXXXXXXXXXXXXXX&contentType=json"
           print('Weather requestUrl={requestUrl}'.format(requestUrl=requestUrl))
           req = urllib.request.urlopen(requestUrl)
           rawForecastData = req.read()
           req.close()
           d = json.loads(rawForecastData)
           time.sleep(5)
   except:
      pass
   return d