Could not get data from the NOAA API calls

482 views Asked by At

I am trying to get access to weather station data following the method mentioned on the website: https://towardsdatascience.com/getting-weather-data-in-3-easy-steps-8dc10cc5c859 I am using python for this. I am still learning python and I have no idea why it's showing error. The lines of code I used is:

r = requests.get('https://www.ncdc.noaa.gov/cdo-web/api/v2/data?datasetid=GHCND&datatypeid=\
                       TAVG&limit=1000&stationid=GHCND:USC00297461&startdate=2020-01-01&enddate=2020-10-22',
                       headers={'token':Token})
d = r.json()
avg_temps = [item for item in d['results'] if item['datatype']=='TAVG']

The error is:

KeyError                                  Traceback (most recent call last)
<ipython-input-85-c1a56919f6ab> in <module>()
      4 d = r.json()
      5 #get the item (average temperature) from the response
----> 6 avg_temps = [item for item in d['results'] if item['datatype']=='TAVG']

KeyError: 'results'

1

There are 1 answers

1
Niceclat On

You are getting an error because you should have this

avg_temps = [item for item in d['results'] if item['datatype']=='TAVG']

instead of this :

avg_temps = [item for item in d['results'] if d['datatype']=='TAVG']

difference is item['datatype'] instead of d['datatype']