0

I am trying to access an API and it is mentioned that it gives in HTML. I went through these answers

(Get html using Python requests?) but I am not getting my results. I just wanted to make sure I am doing it correctly as I am getting error like this ("'{"request":{"category_id":"717234","command":"category"},"data":{"error":"invalid or missing api_key'" Is this API not working ? Is there any way to get HTML data and convert them to CSV or excel? Here is the code which I am using.

import requests
URL = "https://api.eia.gov/category?api_key=YOUR_API_KEY_HERE&category_id=717234"
r = requests.get(url = URL)
r.text[:100]
Ghassen
  • 764
  • 6
  • 14
Avi
  • 1,795
  • 3
  • 16
  • 29

2 Answers2

1

you are using a invalid api the link to your html page is not working :

import requests
URL = "https://api.eia.gov/category?api_key=YOUR_API_KEY_HERE&category_id=717234"
headers = {'Accept-Encoding': 'identity'}
r = requests.get(URL, headers=headers)
print(r.text[:100])

output:

{"request":{"category_id":"717234","command":"category"},"data":{"error":"invalid or missing api_key

i try to change the link of the link with that one given in the answer that you put the link and i get a result :

import requests
URL = "http://www.wrcc.dri.edu/WRCCWrappers.py?sodxtrmts+028815+por+por+pcpn+none+mave+5+01+F"
headers = {'Accept-Encoding': 'identity'}
r = requests.get(URL, headers=headers)
print(r.text[:100])

output:

<!DOCTYPE html>
<HTML>
<HEAD><TITLE>Average of Precipitation, Station id: 028815</TITLE></HEAD>
<BO

as a solution you can use an external api the devoloper mode of that api : https://www.eia.gov//developer// or check this link to get a key :https://www.eia.gov/opendata/

Ghassen
  • 764
  • 6
  • 14
0

Its not an error.
seems to me like youre missing your API KEY.

this is what wrote in the link you put:

{"request":{"category_id":"717234","command":"category"},"data":{"error":"invalid or missing api_key. For key registration, documentation, and examples see https:\/\/www.eia.gov\/developer\/"}}
Almog
  • 29
  • 4
  • Yes thats correct it is mentioned in the website that this Key can be accessed, so I was just thinking if there is any other way or correct code to access it. – Avi Jun 30 '19 at 21:40
  • Which api are you trying to access? – Almog Jun 30 '19 at 21:41