【问题标题】:Python and Json - Best way to pull out sections of data from API?Python 和 Json - 从 API 中提取数据部分的最佳方法?
【发布时间】:2020-10-13 02:12:16
【问题描述】:

想找出从 API 中获取和组织我想要的部分的最佳方法,因为它会显示大量无用的数据。

输入

response = requests.get('https://urlscan.io/api/v1/result/' + uuid + '/')
r3 = response.content.decode("utf-8")
print(r3)

输出:(https://urlscan.io/api/v1/result/1742c69a-17eb-4089-a01e-18decd64579c)

*Only displaying bottom of data*
    "hashes": [
      "c67d9981ef9235ddcc3081be2c92f402b7525e5601970eb4811a3ea941d82448",
      "60b19e5da6a9234ff9220668a5ec1125c157a268513256188ee80f2d2c8d8d36",
      "970c15466fc742d78103ec93f9d78741ae743c6d08fbaec2fb0f541897b53f97",
      "5407e68f9bd5a647a464ebf80920fa1289747821da4497f020ede6c843701178",
      "3990ce6ae2ec606267ea6bee16b21dbd9bf5847dddb6e3f51c3539765c8a5af9",
    ]
  },
  "verdicts": {
    "overall": {
      "score": 0,
      "categories": [],
      "brands": [],
      "tags": [],
      "malicious": false,
      "hasVerdicts": 0
    },
    "urlscan": {
      "score": 0,
      "categories": [],
      "brands": [],
      "tags": [],
      "detectionDetails": [],
      "malicious": false
    },
    "engines": {
      "score": 0,
      "malicious": [],
      "benign": [],
      "maliciousTotal": 0,
      "benignTotal": 0,
      "verdicts": [],
      "enginesTotal": 0
    },
}

我想要很多数据,但也有很多无用的数据。什么是做某事的最佳方式

Hashes:
*hash 1*
*hash 2*
*hash 3...*

Verdict:
Score: 0
Malicious: False

(next part I want, etc, etc)

所以我不会抓住只有部分我想要的部分的整个部分。

【问题讨论】:

    标签: python json api post


    【解决方案1】:

    上面的答案很好,但我也有一个额外的解决方案

    import requests
    import json
    
    req = requests.Session()
    
    r = req.get("API Link").json()
    #instead of json.loads, I used .json() to make it a bit more simple
    
    

    【讨论】:

      【解决方案2】:

      只需导入json 库并使用loads 方法。应该是这样的:

      import requests 
      import json
      
      response = requests.get(URL)
      data = json.loads(response.text)
      

      然后您可以将数据作为普通的 python 字典进行导航。要获取密钥,请使用data.keys(),然后使用data[key] 访问数据。

      【讨论】:

        猜你喜欢
        • 2020-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-16
        • 1970-01-01
        • 1970-01-01
        • 2021-07-17
        • 2021-11-24
        相关资源
        最近更新 更多