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)
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/