python - 我如何访问和操作 api 值
问题描述
我想了解如何访问数据并进行一些修改等,例如仅访问和列出电子邮件等
import requests,json
api = "https://reqres.in/api/users?page=2"
test = requests.get(api)
x = test.json()
data_structure = []
data_structure.append(x)
print(data_structure)
输出 [{'page': 2, 'per_page': 6, 'total': 12, 'total_pages': 2, 'data': [{'id': 7, 'email': 'michael.lawson@reqres. in','first_name':'Michael','last_name':'Lawson','avatar':'https://reqres.in/img/faces/7-image.jpg'},{'id':8 ,'电子邮件':'lindsay.ferguson@reqres.in','first_name':'Lindsay','last_name':'Ferguson','头像':'https://reqres.in/img/faces/8- image.jpg'}, {'id': 9, 'email': 'tobias.funke@reqres.in', 'first_name': 'Tobias', 'last_name': 'Funke', 'avatar': 'https: //reqres.in/img/faces/9-image.jpg'}, {'id': 10, 'email': 'byron.fields@reqres.in', 'first_name': 'Byron', 'last_name': 'Fields', 'avatar': 'https://reqres.in/img/faces/10-image.jpg'}, {'id': 11, 'email' :'george.edwards@reqres.in','first_name':'George','last_name':'Edwards','头像':'https://reqres.in/img/faces/11-image.jpg' }, {'id': 12, 'email': 'rachel.howell@reqres.in', 'first_name': 'Rachel', 'last_name': 'Howell', 'avatar': 'https://reqres. in/img/faces/12-image.jpg'}], 'support': {'url': 'https://reqres.in/#support-heading', 'text': '为了保持 ReqRes 免费,贡献对服务器费用表示赞赏!'}}]https://reqres.in/img/faces/10-image.jpg'}, {'id': 11, 'email': 'george.edwards@reqres.in', 'first_name': 'George', '姓氏':'爱德华兹','头像':'https://reqres.in/img/faces/11-image.jpg'},{'id':12,'电子邮件':'rachel.howell@reqres。 in','first_name':'Rachel','last_name':'Howell','avatar':'https://reqres.in/img/faces/12-image.jpg'}],'support':{ 'url': 'https://reqres.in/#support-heading', 'text': '为了保持 ReqRes 免费,感谢您对服务器成本的贡献!'}}]https://reqres.in/img/faces/10-image.jpg'}, {'id': 11, 'email': 'george.edwards@reqres.in', 'first_name': 'George', '姓氏':'爱德华兹','头像':'https://reqres.in/img/faces/11-image.jpg'},{'id':12,'电子邮件':'rachel.howell@reqres。 in','first_name':'Rachel','last_name':'Howell','avatar':'https://reqres.in/img/faces/12-image.jpg'}],'support':{ 'url': 'https://reqres.in/#support-heading', 'text': '为了保持 ReqRes 免费,感谢您对服务器成本的贡献!'}}]'爱德华兹','头像':'https://reqres.in/img/faces/11-image.jpg'},{'id':12,'电子邮件':'rachel.howell@reqres.in', 'first_name':'Rachel','last_name':'Howell','头像':'https://reqres.in/img/faces/12-image.jpg'}],'support':{'url' : 'https://reqres.in/#support-heading', 'text': '为了保持 ReqRes 免费,感谢您对服务器成本的贡献!'}}]'爱德华兹','头像':'https://reqres.in/img/faces/11-image.jpg'},{'id':12,'电子邮件':'rachel.howell@reqres.in', 'first_name':'Rachel','last_name':'Howell','头像':'https://reqres.in/img/faces/12-image.jpg'}],'support':{'url' : 'https://reqres.in/#support-heading', 'text': '为了保持 ReqRes 免费,感谢您对服务器成本的贡献!'}}]in/#support-heading', 'text': '为了保持 ReqRes 免费,感谢您对服务器成本的贡献!'}}]in/#support-heading', 'text': '为了保持 ReqRes 免费,感谢您对服务器成本的贡献!'}}]
解决方案
首先,我强烈建议您安装 JSON 查看器扩展,这将帮助您了解 API 上发生了什么。
https://chrome.google.com/webstore/detail/json-viewer/gbmdgpbipfallnflgajpaliibnhdgobh?hl=es
然后,您不需要创建新列表,因为 x = test.json() 已经输出了您从 API 带来的同一个字典。
所以你的第一块代码应该是这样的
import requests,json
api = "https://reqres.in/api/users?page=2"
test = requests.get(api)
x = test.json()
然后您可以访问该字典中的所有数据,例如让我们获取所有电子邮件。
为方便起见,您应该使用 JSON 查看器访问 api 链接以查看字典的结构。
要访问电子邮件,我们首先需要访问字典上的“数据”键
import requests,json
api = "https://reqres.in/api/users?page=2"
test = requests.get(api)
x = test.json()
data_list = x["data"]
之后,您可以看到 data_list 是一个新的字典列表,其中包含来自 API 上每个元素的所有数据(在您的情况下是每个 id)。
所以最后,要访问电子邮件,我们需要遍历该列表并从列表中的每个字典中获取“电子邮件”键。
import requests,json
api = "https://reqres.in/api/users?page=2"
test = requests.get(api)
x = test.json()
data_list = x["data"]
for i in data_list:
print(i["email"])
我的朋友,这就是你从 API 获取信息的方式,就像你操作列表和字典一样。
推荐阅读
- amazon-cloudformation - 您的请求已被 EC2 限制,请确保您有足够的 API 速率限制
- ironpython - 如何使用铁python脚本根据if条件获取变量
- java - 如何在 Spring 中返回 Db 保存的 JSON 的 ID
- configuration - Tensorflow 2 对象检测 API 数据增强是否会创建新数据?
- pandas - Pandas 将函数应用于多个列,使用来自另一个数据帧的值
- java - java - 如何不打印数组中已经在java数组的前一行中的元素?
- datepicker - Hijri 数据选择器覆盖 Gregorian Angular 8
- python-3.x - 无法使用 boto3 在 aws s3 中创建文件夹
- git - 我一直在使用来自本地的无效电子邮件进行提交,如何将所有这些更改链接到我的有效电子邮件。我的
- mysql - 当我从 MySQL phpmyadmin 中的子表中删除索引引用时,父表已删除