Python 获取请求打印特定键和值

Python Get Request Printing specific key and value

我是 HTTP 请求的新手,我正在尝试使用 PythonRequests Library 请求 GitHubs API 发出简单的获取请求。

我目前尝试为键值对实现参数

import requests
r = requests.get("https://api.github.com/repos/git/git", params= {'name':name} )
print(name)

显然这是不正确的,因为我收到一个错误 name isn't defined 这很有意义但是我不知道从我想要的键打印特定值而不是打印整个 r.json()响应。

我刚刚尝试使用这个:

import requests
import json
r = requests.get("https://api.github.com/repos/git/git")
data = r.json()

class User:
    def __init__(self, json_def):
        self.__dict__ = json.loads(json_def)

user = User(data)
print(user.size)

但是我收到错误:

TypeError: the JSON object must be str, bytes or bytearray, not 'dict'

您正在检查包含 a server’s response to an HTTP request. 的 Response 对象。从 link,我猜你正在尝试检查内容 从那个回应。所以你可以修改这个代码。

import requests
import json
r = requests.get("https://api.github.com/repos/git/git")
data = json.loads(r.content)

class User:
    def __init__(self, json_def):
        self.__dict__ = data