简体   繁体   English

我如何访问和操作 api 值

[英]How do i access and manipulate the api values

I would like some direction on how i can access the data and do some modifications etc. for example accessing and listing only emails, etc please我想了解如何访问数据并进行一些修改等,例如仅访问和列出电子邮件等

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)

Output [{'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, 'email': 'lindsay.ferguson@reqres.in', 'first_name': 'Lindsay', 'last_name': 'Ferguson', 'avatar': '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', 'avatar': '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'}], 's Output [{'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' :'字段','头像':'https://reqres.in/img/faces/10-image.jpg'},{'id':11,'电子邮件':'george.edwards@reqres.in' , 'first_name': 'George', 'last_name': 'Edwards', '头像': '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'}],'s upport': {'url': 'https://reqres.in/#support-heading', 'text': 'To keep ReqRes free, contributions towards server costs are appreciated!'}}] upport': {'url': 'https://reqres.in/#support-heading', 'text': '为了保持 ReqRes 免费,感谢您对服务器成本的贡献!'}}]

First, I highly recommend you to install the JSON Viewer extension, which will help you a lot to see what's going on your API.首先,我强烈建议您安装 JSON 查看器扩展,它将帮助您了解 API 发生了什么。

https://chrome.google.com/webstore/detail/json-viewer/gbmdgpbipfallnflgajpaliibnhdgobh?hl=es https://chrome.google.com/webstore/detail/json-viewer/gbmdgpbipfallnflgajpaliibnhdgobh?hl=es

Then, you don't need to create a new list, since the x = test.json() already outputs the same dictionary you brought from the API.然后,您不需要创建新列表,因为 x = test.json() 已经输出了您从 API 带来的同一个字典。

So your first chunk of code should look like this所以你的第一块代码应该是这样的

import requests,json

api = "https://reqres.in/api/users?page=2"

test = requests.get(api)
x = test.json()

Then you can access all the data inside that dictionary, for example let's get all the emails.然后您可以访问该字典中的所有数据,例如让我们获取所有电子邮件。

To make it easier you should go to the api link using JSON Viewer to see the structure of your dictionary.为了更容易,您应该使用 JSON 查看器将 go 连接到 api 链接以查看字典的结构。

To access the emails we first need to access the "data" key on your dictionary要访问电子邮件,我们首先需要访问字典上的“数据”键

import requests,json

api = "https://reqres.in/api/users?page=2"

test = requests.get(api)
x = test.json()

data_list = x["data"]

After that you can see that data_list is a new list of dictionaries with all the data from each element on your API (in your case each id).之后,您可以看到 data_list 是一个新的字典列表,其中包含 API 上每个元素的所有数据(在您的情况下是每个 id)。

So finally, to access the emails, we need to iterate trough that list and get the "email" key from each dictionary on the list.所以最后,要访问电子邮件,我们需要遍历该列表并从列表中的每个字典中获取“电子邮件”键。

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"])

And that my friend, is how you get info from an API, the same way you manipulate lists and dictionaries.我的朋友,就是你从 API 获取信息的方式,就像你操作列表和字典一样。

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

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