我如何查询这个(json看)数据?

时间:2017-08-15 10:30:46

标签: python json

我通过api获得这些数据,我希望得出那里的所有“信心”分数。我将如何做到这一点:

{
  "time": 765,
  "annotations": [
    {
      "start": 106,
      "end": 115,
      "spot": "customers",
      "confidence": 0.7198,
      "id": 234206,
      "title": "Customer",
      "uri": "h''ttp://en.wikipedia.org/wiki/Customer",
      "label": "Customer"
    },
    {
      "start": 116,
      "end": 122,
      "spot": "online",
      "confidence": 0.6313,
      "id": 41440,
      ''"title": "Online and offline",
      "uri": "http://en.wikipedia.org/wiki/Online_and_offline",
      "label": "Online"
    },
    {
      "start": 138,
      "end": 143,
      "s''pot": "small",
      "confidence": 0.7233,
      "id": 276495,
      "title": "Small business",
      "uri": "http://en.wikipedia.org/wiki/Small_business",
      "label''": "Small business"
    }
  ]
}

我尝试过像data["confidence"]这样的事情,但它似乎没有拉出信心数字。如何提取Python中的置信度数据?先感谢您。

这是检索数据的代码:

import requests

api_key = INSERT API KEY

content_urls = "http://www.startupdonut.co.uk/sales-and-marketing/promote-your-business/using-social-media-to-promote-your-business"

url = "https://api.dandelion.eu/datatxt/nex/v1/?lang=en &url="+content_urls+"&token=" + api_key

request = requests.get(url)

for data in request:
    print (data)

1 个答案:

答案 0 :(得分:1)

您的API响应数据如下所示:

{
  "time": 765,
  "annotations": [
    {
      "start": 106,
      "end": 115,
      "spot": "customers",
      "confidence": 0.7198,
      "id": 234206,
      "title": "Customer",
      "uri": "h''ttp://en.wikipedia.org/wiki/Customer",
      "label": "Customer"
    },
    {
      "start": 116,
      "end": 122,
      "spot": "online",
      "confidence": 0.6313,
      "id": 41440,
      ''"title": "Online and offline",
      "uri": "http://en.wikipedia.org/wiki/Online_and_offline",
      "label": "Online"
    }
  ]
}

要提取条件,您可以使用以下代码:

import json
# ... request formation code goes here
response = requests.get(url)
data = response.json() # OR data = json.loads(response.content)
confidence_values = []
for annotation in data['annotations']:
    confidence_values.append(annotation.get('confidence'))

print(confidence_values)