简体   繁体   English

如何使用 Pandas 导入 API 数据?

[英]How to import API data using Pandas?

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:我正在尝试从 EIA API 中提取一些数据,以下是我尝试过的,但在第一行代码中出现错误:

AttributeError: 'str' object has no attribute 'text' AttributeError:“str”对象没有属性“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))

You haven't requested anything from the API.您尚未从 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.有 2 个=符号,因此您真正要做的是将 URL 字符串分配给call_eia变量和requests模块的get属性,从而覆盖最初存在的函数。 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.然后,当您尝试将call_eia传递给pd.read_csv()时,不是传递requests对象,而是传递一个字符串,即 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.相反,您的代码应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM