英文:
Unable to connect with EIA api url
问题
我尝试使用GET方法从EIA API URL检索数据,但无法建立连接。
import pandas as pd
import requests
call_eia = requests.get("https://api.eia.gov/v2/petroleum/pri/spt/data/?frequency=daily&data[0]=value&facets[series][]=RBRTE&sort[0][column]=period&sort[0][direction]=desc&offset=0&length=5000")
print(call_eia)
当前在打印输出中获得<Response [403]>。
英文:
I'm trying to retrieve data via EIA api url with the get method but unable to establish connection.
import pandas as pd
import requests
call_eia = requests.get("https://api.eia.gov/v2/petroleum/pri/spt/data/?frequency=daily&data[0]=value&facets[series][]=RBRTE&sort[0][column]=period&sort[0][direction]=desc&offset=0&length=5000")
print(call_eia)
currently getting <Response [403]> as output of print
答案1
得分: 0
requests.get
传递的URL返回以下自说明消息:
> API_KEY_MISSING
> 未提供api_key。请在 https://www.eia.gov/opendata/register.php 注册一个。
如 API 技术文档 所述,您需要将 api_key
参数插入请求中:
import requests
params = {'api_key': 'your_api_key'}
call_eia = requests.get("https://api.eia.gov/v2/petroleum/pri/spt/data/?frequency=daily&data[0]=value&facets[series][]=RBRTE&sort[0][column]=period&sort[0][direction]=desc&offset=0&length=5000", params=params)
print(call_eia)
文档还描述了如何获取 API 密钥。
英文:
Hitting the URL passed to requests.get
returns the self-explanatory message:
> API_KEY_MISSING
> No api_key was supplied. Please register for one at https://www.eia.gov/opendata/register.php
As described in API Technical Documentation
you need to insert api_key
parameter to the request:
import requests
params = {'api_key': 'your_api_key'}
call_eia = requests.get("https://api.eia.gov/v2/petroleum/pri/spt/data/?frequency=daily&data[0]=value&facets[series][]=RBRTE&sort[0][column]=period&sort[0][direction]=desc&offset=0&length=5000", params=params)
print(call_eia)
The documentation also describes how to obtain the API key.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论