Python Forum

Full Version: Python API and requests
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,
I'm trying to download some data from the Energy Information Administration with my API. This url: monthly electricity generation has a dataset I'd like to use for some research.

If you follow the example they have: http://api.eia.gov/series/?api_key=YOUR_...LL-CA-99.M and insert the API KEY they have given me, I don't get any download or response in my IPYTHON shell. It's just blank.

import requests

In [2]: response=requests.get("http://api.eia.gov/series/?api_key=YOUR_API_KEY_HERE&series_id=ELEC.GEN.ALL-CA-99.M")
Operator error? Like I said it's blank.

Thanks for any help,
You can get data by accessing .content attribute, e.g. response.content, and/or check status_code first, e.g. ensure that response.status_code is 200.

import json
import requests
response=requests.get("Your url with  API key (as you posted above)")
if response.status_code == 200:
    data = json.loads(response.content)
else:
    print("Some error occurred... ")
Thanks scidam. I still get a blank though after I code it in.

No error gets thrown up on my console.

Not sure where to go from here.
I ran the code with your API KEY and obtained the data. What is output of the following code on your computer:
import requests
resp = requests.get('url with your API KEY')
print(f"Status code: {resp.status_code}")
print(f"Data length: {len(resp.content)}")
@scidam: I ran your code. I get Status code:200 and Data length: 5774.

So, the Data length is showing me data that is downloaded? Or is this another status code?

Many thx for the tutorial.
No, that value is presumably just the size of the body in bytes. Did you read the docs for Requests to see how to get the body?
@ndc85430:
I have been reading the link you posted on the docs for requests. I can't seem to find the code or the command for retrieving the body. In the docs they show you how to retrieve POST, JSON, etc, but how do I determine if that's the data that the API has? Do you need to have this in your code?

Thanks for the help.
(Jul-29-2020, 01:48 PM)deep_logic Wrote: [ -> ]In the docs they show you how to retrieve POST, JSON, etc, but how do I determine if that's the data that the API has? Do you need to have this in your code?

Thanks for the help.
It almost what @scidam describe,but don't need import json as Requests has this build in.
As i can look at history in Thread can test with you Api Key.
import requests

api_key = 'xxxxxxxxxxxxxxxxx'
response = requests.get(f"http://api.eia.gov/series/?api_key={api_key}&series_id=ELEC.GEN.ALL-CA-99.M")
print(response.status_code)
json_data = response.json()
# print(json_data) ## Look at json data getting back

# Get some data out the dicionray
print(json_data['request']['series_id'])
print(json_data['series'][0]['data'][0:3])
Output:
200 ELEC.GEN.ALL-CA-99.M [['202005', 15465.1299], ['202004', 14097.84274], ['202003', 14823.10536]]
So if i take quick look at the API doc for this so is functionality simple do a GET requests and get JSON back.
So if i do test for Petroleum barrel prices.
import requests

api_key = 'xxxxxxxxxxxx'
response = requests.get(f"http://api.eia.gov/series/?api_key={api_key}&series_id=PET.RWTC.A")
json_data = response.json()
print(json_data['series'][0]['units'])
print(json_data['series'][0]['data'][0:4])
Output:
Dollars per Barrel [['2019', 57], ['2018', 65.23], ['2017', 50.8], ['2016', 43.29]]
starting from your code:
response=requests.get("http://api.eia.gov/series/?api_key=YOUR_API_KEY_HERE&series_id=ELEC.GEN.ALL-CA-99.M")
response variable will hold Response Object.
You can retrieve different attributes like text and pass it to json.loads() as suggested by scidam.
or use convenient method resposnse.json()

import requests

URL = 'YOUR URL HERE'
response=requests.get(URL)
data = response.json()
print(data)
or you can do

import requests

API_KEY = 'YOUR_API_KEY'

payload = {'api_key':API_KEY,
          'series_id':'ELEC.GEN.ALL-CA-99.M'}
URL = 'http://api.eia.gov/series/'
response=requests.get(URL, params=payload)
data = response.json()
print(data)
(Jul-29-2020, 01:48 PM)deep_logic Wrote: [ -> ]In the docs they show you how to retrieve POST, JSON, etc, but how do I determine if that's the data that the API has? Do you need to have this in your code?

Of course the data is going to be in the response body. Where else would it be? As shown by buran, response.json() parses the body into a Python dictionary.