无法连接到EIA API URL。

huangapple go评论72阅读模式
英文:

Unable to connect with EIA api url

问题

我尝试使用GET方法从EIA API URL检索数据,但无法建立连接。

数据URL= https://www.eia.gov/opendata/browser/petroleum/pri/spt?frequency=daily&data=value;&facets=series;&series=RWTC;&sortColumn=period;&sortDirection=desc;

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.

data URL= https://www.eia.gov/opendata/browser/petroleum/pri/spt?frequency=daily&amp;data=value;&amp;facets=series;&amp;series=RWTC;&amp;sortColumn=period;&amp;sortDirection=desc;

import pandas as pd
import requests
call_eia = requests.get(&quot;https://api.eia.gov/v2/petroleum/pri/spt/data/?frequency=daily&amp;data[0]=value&amp;facets[series][]=RBRTE&amp;sort[0][column]=period&amp;sort[0][direction]=desc&amp;offset=0&amp;length=5000&quot;)

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 = {&#39;api_key&#39;: &#39;your_api_key&#39;}
call_eia = requests.get(&quot;https://api.eia.gov/v2/petroleum/pri/spt/data/?frequency=daily&amp;data[0]=value&amp;facets[series][]=RBRTE&amp;sort[0][column]=period&amp;sort[0][direction]=desc&amp;offset=0&amp;length=5000&quot;, params=params)

print(call_eia)

The documentation also describes how to obtain the API key.

huangapple
  • 本文由 发表于 2023年6月12日 05:48:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定