[Python] Use of parameters dictionary with Python requests GET method

Discussão em 'Python' iniciado por Stack, Abril 11, 2025.

  1. Stack

    Stack Membro Participativo

    Trying to retrieve data via the EIA data API (v2): https://www.eia.gov/opendata/documentation.php.

    I'm able to use the API dashboard to return data:

    https://www.eia.gov/opendata/browse...nthly&data=price;revenue;sales;&start=2013-01

    But when I attempt to retrieve within Python using the attached documentation, I don't appear to be returning any values when using the same parameters.

    url = 'https://api.eia.gov/v2/electricity/retail-sales/data/?api_key=' + API_KEY

    params = {
    "frequency": "monthly",
    "data": [
    "revenue",
    "sales",
    "price"
    ],
    "start": "2013-01"
    }

    if x.status_code == 200:
    print('Success')
    else:
    print('Failed')

    res = x.json()['response']
    data = res['data']


    If I print the url created by the GET method, and compare to API url included in the dashboard, the issue appears to be in the way the GET method is attempting to retrieve items from the data parameter:

    Works

    https://api.eia.gov/v2/electricity/retail-sales/data/?frequency=monthly&data[0]=price&data[1]=revenue&data[2]=sales&start=2013-01&sort[0][column]=period&sort[0][direction]=desc&offset=0&length=5000

    Doesn't work (returned by GET method):

    https://api.eia.gov/v2/electricity/...a=revenue&data=sales&data=price&start=2013-01

    Can anyone provided guidance on how to coerce the GET method to pass my data parameters in the same way as the API dashboard appears to?

    Continue reading...

Compartilhe esta Página