pandas 无法与EIA API url连接

pn9klfpd  于 2023-06-20  发布在  其他
关注(0)|答案(1)|浏览(112)

我试图检索数据通过EIA API URL与获取方法,但无法建立连接。
data 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]>作为print的输出

bpsygsoo

bpsygsoo1#

点击传递给requests.get的URL返回不言自明的消息:
API_KEY_MISSING未提供api_key。请在https://www.eia.gov/opendata/register.php注册
API Technical Documentation中所述,您需要在请求中插入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密钥。

相关问题