为了账号安全,请及时绑定邮箱和手机立即绑定

Python从请求响应中获取第一项

Python从请求响应中获取第一项

慕盖茨4494581 2023-03-08 16:27:08
我正在使用这样的 python 请求查询 API...url = "www.example.com"response = requests.request("POST", url)response--------[  {    "id": "485744",    "descript": "firstitem",  },  {    "id": "635456",    "descript": "seconditem",  },  {    "id": "765554",    "descript": "thirditem",  },]我正在尝试像这样访问响应中的第一项...response = response.jsonprint(response[0])但我收到错误...TypeError: 'method' object is not subscriptable我哪里错了?
查看完整描述

2 回答

?
开心每一天1111

TA贡献1836条经验 获得超13个赞

json是类的方法Response而不是对象的属性。要获取 json 数据,请使用:

response = response.json()


查看完整回答
反对 回复 2023-03-08
?
繁星coding

TA贡献1797条经验 获得超4个赞

你没有正确地称呼它:response.json()


替代方法:


import json #import this on the top

response = json.loads(response) #You need to load the data into JSON

还:每个末尾的附加逗号 (,) "descript":


"firstitem", "seconditem","thirditem"不是必需的,可能会出错。


查看完整回答
反对 回复 2023-03-08
  • 2 回答
  • 0 关注
  • 73 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信