Home > database >  How to import API data using Pandas?
How to import API data using Pandas?

Time:05-25

I am trying to pull some data from EIA API, below is what I tried but I'm getting the error on the first line of code:

AttributeError: 'str' object has no attribute 'text'

Any help would be much appreciated!

call_eia = requests.get = 'https://api.eia.gov/v2/nuclear-outages/facility-nuclear-outages/data?api_key=XXXXXXXX'
data_eia=pd.read_csv(StringIO(call_eia.text))

CodePudding user response:

You haven't requested anything from the API. Look carefully at your first line:

call_eia = requests.get = 'https://api.eia.gov/v2/nuclear-outages/facility-nuclear-outages/data?api_key=XXXXXXXX'
#        ^              ^

There are 2 = signs, so what you're really doing is assigning the URL string to both your call_eia variable and the get attribute of the requests module, overwriting the function that was there originally. Then, when you try to pass call_eia to pd.read_csv(), instead of passing a requests object, you're just passing a string, the URL.

Try

call_eia = requests.get('https://api.eia.gov/v2/nuclear-outages/facility-nuclear-outages/data?api_key=XXXXXXXX')

instead and your code should work.

  • Related