【问题标题】:Print Specific Value from an API Request in Python从 Python 中的 API 请求中打印特定值
【发布时间】:2020-09-15 09:03:15
【问题描述】:

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

这是我目前所拥有的:

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获取请求导出如下:

[
    {
        **"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 请求中,我试图获取嵌套的“登录”和 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))

【问题讨论】:

    标签: python json python-3.x api github


    【解决方案1】:

    由于它是一个 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')
    

    【讨论】:

    • 谢谢。我稍微修改了一下,但它完全有效。
    【解决方案2】:

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

    
    response = requests.get(...).json()
    
    login = response[0]['user']
    

    【讨论】:

      【解决方案3】:

      您可以将 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']
      

      【讨论】:

        猜你喜欢
        • 2017-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多