在Python中打印API请求中的特定值

2024-05-17 05:42:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试打印API请求中的值。返回的JSON文件很大(4000行),所以我只是尝试从键值对中获取特定值并自动生成消息

以下是我到目前为止的情况:

import requests
import json
import urllib


url = "https://api.github.com/repos/<companyName>/<repoName>/issues" #url 

payload = {}
headers = {
  'Authorization': 'Bearer <masterToken>' #authorization works fine 
}
name = (user.login) #pretty sure nothing is being looked out
url = (url)

print(hello %name, you have a pull request to view. See here %url for more information) # i want to print those keys here

JSON文件(从API get请求导出)如下所示:

[
    {
        **"url": "https://github.com/<ompanyName>/<repo>/issues/1000",**
        "repository_url": "https://github.com/<ompanyName>/<repo>",
        "labels_url": "https://github.com/<ompanyName>/<repo>/issues/1000labels{/name}",
        "comments_url": "https://github.com/<ompanyName>/<repo>/issues/1000",
        "events_url": "https://github.com/<ompanyName>/<repo>/issues/1000",
        "html_url": "https://github.com/<ompanyName>/<repo>/issues/1000",
        "id": <id>,
        "node_id": "<nodeID>",
        "number": 702,
        "title": "<titleName>",
        "user": {
            **"login": "<userName>",**
            "id": <idNumber>,
            "node_id": "nodeID",
            "avatar_url": "https://avatars3.githubusercontent.com/u/urlName?v=4",
            "gravatar_id": "",
            "url": "https://api.github.com/users/<userName>",
            "html_url": "https://github.com/<userName>",
            "followers_url": "https://api.github.com/users/<userName>/followers",
            "following_url": "https://api.github.com/users/<userName>/following{/other_user}",
            "gists_url": "https://api.github.com/users/<userName>/gists{/gist_id}",
            "starred_url": "https://api.github.com/users/<userName>/starred{/owner}{/repo}",
            "subscriptions_url": "https://api.github.com/users/<userName>/subscriptions",
            "organizations_url": "https://api.github.com/users/<userName>/orgs",
            "repos_url": "https://api.github.com/users/<userName>/repos",
            "events_url": "https://api.github.com/users/<userName>/events{/privacy}",
            "received_events_url": "https://api.github.com/users/<userName>/received_events",
            "type": "User",
            "site_admin": false
        },
]

(注意,这个JSON文件会重复几百次) 从API请求中,我试图获取嵌套的“login”和url。 我错过了什么? 谢谢

编辑:

已解决:

import requests
import json
import urllib


url = "https://api.github.com/repos/<companyName>/<repoName>/issues"

payload = {}
headers = {
  'Authorization': 'Bearer <masterToken>'
}

response = requests.get(url).json()
for obj in response:
    name = obj['user']['login']
    url = obj['url']
    print('Hello {0}, you have an outstanding ticket to review. For more information see here:{1}.'.format(name,url))

Tags: namehttpsimportgithubcomapiidurl
3条回答

因为它是一个JSON数组,所以必须在它上面循环。JSON对象被转换为字典,因此您可以使用['key']来访问元素

for obj in response:
    name = obj['user']['login']
    url = obj['url']
    print(f'hello {name}, you have a pull request to view. See here {url} for more information')

您可以将JSON格式的数据转换为Python字典,如下所示: https://www.w3schools.com/python/python_json.asp

json_data = ... # response from API
dict_data = json.loads(json_data)

login = response[0]['user']['login']
url = response[0]['url']

您可以将其解析为python列表/字典,然后像访问任何其他python对象一样访问它


response = requests.get(...).json()

login = response[0]['user']

相关问题 更多 >