【问题标题】:Extract Id's from response.text()从 response.text() 中提取 ID
【发布时间】:2021-07-22 06:03:51
【问题描述】:

我想从以下响应(查找)中提取 id 的所有值。

代码:

findings= requests.get('https://api.probely.com/targets/TaargetID/findings/36',headers={"Content-Type": "application/json", "Authorization": "JWT <Token>"})
finding=findings.json()

发现:

{"count":45,
   "page_total":5,
   "page":1,
   "length":10,
   "results":[
      {
         "id":79,
         "target":
             {"id":"RzXFSNHH3qUY","name":"","site": 
                 {"id":"2qk21XKLrKyf","url":"https://test- 
                 0.ox.qa.prbly.win",.....},
          "(...)"
         ,
         "id":12,
         "target":
          "(...)"
         ,
         "id":32,
         "target":
          "(...)"
         ,"(....)"
}
]
}

我想得到一个这样的 id 数组: id=[79,12,32] 如何提取这些 Id 并存储在数组中?

【问题讨论】:

    标签: python curl python-requests libcurl pycurl


    【解决方案1】:

    您可以使用迭代来遍历“结果”中的每个项目,然后将 ID 保存到数组中。

    id_list = []
    
    for result in finding["results"]:
        id_list.append(result["id"])
    

    我还想指出,您不应调用任何变量 id,因为它是 Python 内置函数,覆盖它可能会产生不良后果。

    【讨论】:

    • 我怎样才能得到id:79的url?
    【解决方案2】:

    你可以试试:

    ids = list(map(lambda res: res["id"], finding["results"]))
    

    什么类型的“结果”键? “id”字段出现多次

    【讨论】:

    • 我怎样才能得到id:79的url?
    猜你喜欢
    • 1970-01-01
    • 2015-03-20
    • 2016-11-26
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多