Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python API and requests
#1
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,
"Human history becomes more and more a race between education and catastrophe." - H. G. Wells (1866-1946)
Reply
#2
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... ")
Reply
#3
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.
"Human history becomes more and more a race between education and catastrophe." - H. G. Wells (1866-1946)
Reply
#4
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)}")
Reply
#5
@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.
"Human history becomes more and more a race between education and catastrophe." - H. G. Wells (1866-1946)
Reply
#6
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?
Reply
#7
@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.
"Human history becomes more and more a race between education and catastrophe." - H. G. Wells (1866-1946)
Reply
#8
(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]]
Reply
#9
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python requests oauth2 access token herobpv 6 1,474 Sep-27-2021, 06:54 PM
Last Post: herobpv
  Python Requests SSL Aussie 0 1,098 Jan-07-2021, 02:09 AM
Last Post: Aussie
  Python Requests Aussie 2 1,497 Dec-23-2020, 03:24 AM
Last Post: Aussie
  Python Requests package: Handling xml response soumyarani 1 1,202 Sep-14-2020, 11:41 AM
Last Post: buran
  How to save Python Requests data sent to server? RedLeonard 5 2,574 Jul-05-2020, 10:33 AM
Last Post: RedLeonard
  How to do the same as python -m requests.certs inside script? geekgeek 2 1,401 Feb-27-2020, 07:11 AM
Last Post: buran
  Python requests writes the name of the file instead of contents to web page bluethundr 1 1,501 Jun-05-2019, 09:35 PM
Last Post: Larz60+
  Python requests module doesn not return status_code palo173 5 2,212 May-21-2019, 09:22 AM
Last Post: buran
  Curl command to python requests pythonclass 0 2,603 Apr-12-2019, 06:29 PM
Last Post: pythonclass
  C++ programmer requests Python Web/database advice GregReese 3 1,414 Apr-10-2019, 09:41 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020