[英]Python getting first item from requests reponse
I am querying an API using python requests like this...我正在使用这样的 python 请求查询 API ...
url = "www.example.com"
response = requests.request("POST", url)
response
--------
[
{
"id": "485744",
"descript": "firstitem",
},
{
"id": "635456",
"descript": "seconditem",
},
{
"id": "765554",
"descript": "thirditem",
},
]
I am trying to access the first item in the response like this...我正在尝试像这样访问响应中的第一项...
response = response.json
print(response[0])
But I am getting the error...但我得到了错误......
TypeError: 'method' object is not subscriptable
Where am I going wrong?我哪里错了?
json
is a method of the Response
class and not an attribute of the object. json
是Response
class 的方法,而不是 object 的属性。 To get the json data, use:要获取 json 数据,请使用:
response = response.json()
You haven't properly called it: response.json()
您没有正确调用它: response.json()
Alternate method:替代方法:
import json #import this on the top
response = json.loads(response) #You need to load the data into JSON
ALSO : The additional commas (,) at the end of each "descript"
:另外:每个"descript"
末尾的附加逗号(,):
"firstitem"
, "seconditem"
, "thirditem"
is not required and may give errors. "firstitem"
, "seconditem"
seconditem" , "thirditem"
不是必需的,可能会出错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.