简体   繁体   English

从 response.text() 中提取 ID

[英]Extract Id's from response.text()

I want to extract all the values of id's from the following response (finding).我想从以下响应(查找)中提取 id 的所有值。

code:代码:

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

finding:发现:

{"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":
          "(...)"
         ,"(....)"
}
]
}

I want to get an array of id's like this: id=[79,12,32] How can I extract these Id's and store in an array?我想得到一个这样的 id 数组: id=[79,12,32]如何提取这些 Id 并存储在数组中?

You can use iteration to go over each item in "results", then save the IDs to an array.您可以对“结果”中的每个项目使用 go 迭代,然后将 ID 保存到数组中。

id_list = []

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

I'd also like to point out that you shouldn't call any variable id as it is a built in Python function, overwriting it could have bad consequences.我还想指出,您不应该调用任何变量id ,因为它是内置的 Python function,覆盖它可能会产生不良后果。

You can try it:你可以试试:

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

What type of "results" key?什么类型的“结果”键? "id" field occurs several times “id”字段出现多次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM