那么python如何在post请求中调用"next“页面呢?
我知道我需要做什么,但不知道如何实现,youtube中的所有示例都使用#,而不是游标,作为半初学者,我有点困惑
到目前为止,这是我的代码:
def main_request(headers, url1, params):
    response = requests.post(url1, headers=headers, json=params, verify=False)
    jsonData = response.json()
    has_next_key = False
    nextKey = ""
    if "next_key" in jsonData:
        next = True
        nextKey = jsonData["next"]
    while has_next_key:
        data = {"limit_count":500, "limit_size":10000,"curr_key":nextKey}
        params = {"data":json.dumps(data, separators=(",", ":"))}
        req = requests.post(url1, headers=headers, json=params, verify=False)  ## this should do GET request for the third page and so on...
        if "next_key" in req:
            nextKey = req["next_key"]
            print(nextKey) # this returns "3321" which is the value for "next_key" in second page
        else:
            has_next_key = False
            # no next_key, stop the loop这是在每个请求的末尾返回的值
{
"data": [],
"metadata": {},
"links": [
{
"href": "https://us.api.insight.rapid7.com:443/vm/v4/integration/assets?page=0&size=2",
"rel": "first"
},
{
"href": "https://us.api.insight.rapid7.com:443/vm/v4/integration/assets?page=0&size=2",
"rel": "self"
},
{
"href": "https://us.api.insight.rapid7.com:443/vm/v4/integration/assets?page=1&size=2&cursor=1542252837:::_S:::12474375-34a7-40a3-9821-28db0b5cc90e-default-asset-10",
"rel": "next"
},
{
"href": "https://us.api.insight.rapid7.com:443/vm/v4/integration/assets?page=1097&size=2",
"rel": "last"
}
]
}根据rapid7支持,我需要使用游标值
发布于 2022-10-25 19:30:42
给定您的jsonData作为输入,您可以使用以下代码获取下一个url并将其分配给url1
for item in jsonData.get("links", []):
    if item["rel"] == "next":
        url1 = item["href"]
        break这只会找到第一个网址。如果您需要所有urls,我建议将所有urls添加到列表中。例如:
links = [item["href"] for item in jsonData.get("links", []) if item["rel"] == "next"]https://stackoverflow.com/questions/74199197
复制相似问题