December 2018
Intermediate to advanced
226 pages
4h 57m
English
Here, we see a generic example of how to access an API and parse some basic values from the return values:
import requestscity="london"#this would give a sample data of the city that was used in the variable urlx="https://samples.openweathermap.org/data/2.5/weather?q="+city+"&appid=b6907d289e10d714a6e88b30761fae22"#send the request to URL using GET Method r = requests.get(url = urlx) output=r.json()#parse the valuable information from the return JSON print ("Raw JSON \n") print (output) print ("\n")#fetch and print latitude and longitude citylongitude=output['coord']['lon'] citylatitude=output['coord']['lat'] print ("Longitude: "+str(citylongitude)+" , "+"Latitude: "+str(citylatitude))
The sample output is as follows:
>>>
Read now
Unlock full access