python - 无法从 NOAA API 调用中获取数据
问题描述
我正在尝试按照网站上提到的方法访问气象站数据:https ://towardsdatascience.com/getting-weather-data-in-3-easy-steps-8dc10cc5c859我为此使用python。我还在学习python,我不知道为什么它会显示错误。我使用的代码行是:
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']
错误是:
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'
解决方案
你得到一个错误,因为你应该有这个
avg_temps = [item for item in d['results'] if item['datatype']=='TAVG']
而不是这个:
avg_temps = [item for item in d['results'] if d['datatype']=='TAVG']
区别item['datatype']
是d['datatype']
推荐阅读
- reactjs - 如何在 React Native 中实现类似的功能?
- python - 如何将窗口大小调整为 selenium 上的 deviceEmulator 尺寸
- mysql - 将sql查询组合在一起
- javascript - 控制 DOM 中的每个“src”属性更改
- javascript - 未捕获的 ReferenceError: sel 未在 HTMLDivElement.onclick 中定义
- html - 如果元素定位已经受到容器 div 的影响,如何在 CSS 中创建下拉菜单?
- android - 当应用程序因来电事件关闭时,如何发送通知?
- c# - 如何根据对象的出现改变边框的样式?
- powershell - 为什么这两个 PowerShell 脚本的“调用命令”运行时间如此不同
- google-apps-marketplace - 谷歌市场应用程序安装后如何自定义成功屏幕?