【问题标题】:Making a GET request JSON with parameters using Python使用 Python 生成带有参数的 GET 请求 JSON
【发布时间】:2016-07-31 03:19:56
【问题描述】:

我想知道如何使用两个查询参数向特定 url 发出 GET 请求?这些查询参数包含两个 id 号 到目前为止,我有:

import json, requests
url = 'http://'
requests.post(url)

但他们给了我查询参数 first_id=### 和 last_id=###。我不知道如何包含这些参数?

【问题讨论】:

    标签: python json get


    【解决方案1】:

    要发出 GET 请求,您需要 get() method,参数使用 params 参数:

    response = requests.get(url, params={'first_id': 1, 'last_id': 2})
    

    如果响应是 JSON 内容类型,您可以使用 json() 快捷方式将其加载到 Python 对象中:

    data = response.json()
    print(data)
    

    【讨论】:

    • 感谢您的回复!如何获取 JSON 响应本身?抱歉,这是我第一次发出 GET 请求
    • 是 response.json 吗?所以打印 response.json?
    • 啊,我明白了!谢谢!
    猜你喜欢
    • 2023-03-30
    • 2014-02-15
    • 1970-01-01
    • 2023-02-11
    • 2023-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    相关资源
    最近更新 更多