python3请求接口并格式化json输出

环境

python3
IDE:VS Code

代码

import requests
import json


def queryApp():

    url = "https://www.v2ex.com/api/nodes/show.json"

    querystring = {"name":"python"}

    headers = {
        'cache-control': "no-cache"
        }

    response = requests.request("GET", url, headers=headers, params=querystring)
    dat = json.loads(response.text)
    js = get_pretty_print(dat)
    print(js)

#格式化json输出
def get_pretty_print(json_object):
    return json.dumps(json_object, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False)

queryApp()

猜你喜欢

转载自blog.csdn.net/weixin_43169720/article/details/84953626