0

I am not very familiar with API calls or the requests module. I am trying to get the about information (details) for each DAO. I correctly get the names of the DAOs but I get KeyError when I try to do the details. Any help would be greatly appreciated.

import pandas as pd
import requests

payload = {"requests": [{"indexName": "governance_production", "params": "highlightPreTag=%3Cais-highlight-0000000000%3E&highlightPostTag=%3C%2Fais-highlight-0000000000%3E&hitsPerPage=855&attributesToRetrieve=%5B%22id%22%5D&maxValuesPerFacet=100&query=&page=0&facets=%5B%22types%22%2C%22tags%22%5D&tagFilters="}]}
url = 'https://3b439zgym3-2.algolianet.com/1/indexes/*/queries?x-algolia-agent=Algolia%20for%20JavaScript%20(3.35.1)%3B%20Browser%20(lite)&x-algolia-application-id=3B439ZGYM3&x-algolia-api-key=14a0c8d17665d52e61167cc1b2ae9ff1'
headers = {"content-type": "application/x-www-form-urlencoded"}
req = requests.post(url, headers=headers, json=payload).json()

data = []

for item in req['results'][0]['hits']:
    data.append({
        "name": item['_highlightResult']['name']['value'],
        "details": item['_highlightResult']['details']['value'],
    })

print(data)
df = pd.DataFrame(data)
print(df)
1
  • Is the above code from an existing answer on SO? Is there more context to the API call? Commented May 27, 2022 at 18:04

1 Answer 1

1

Because there is no key named details exists in the resulted JSON, that's why it returns an error.

Here is a sample from the request you made above -

Either it includes tags key along with name and types

  {
    "_highlightResult": {
      "assetSlug": {
        "matchLevel": "none",
        "matchedWords": [],
        "value": "tribe"
      },
      "name": {
        "matchLevel": "none",
        "matchedWords": [],
        "value": "Fei"
      },
      "tags": [
        {
          "matchLevel": "none",
          "matchedWords": [],
          "value": "DeFi"
        }
      ],
      "types": [
        {
          "matchLevel": "none",
          "matchedWords": [],
          "value": "Protocol"
        }
      ]
    },
    "id": "f9779bc3-4eb4-4830-982b-fc981762dbd8",
    "objectID": "f9779bc3-4eb4-4830-982b-fc981762dbd8"
  }

or not including tags key

  {
    "_highlightResult": {
      "assetSlug": {
        "matchLevel": "none",
        "matchedWords": [],
        "value": "aave"
      },
      "name": {
        "matchLevel": "none",
        "matchedWords": [],
        "value": "Aave Grants DAO"
      },
      "types": [
        {
          "matchLevel": "none",
          "matchedWords": [],
          "value": "Grants"
        }
      ]
    },
    "id": "b3a88880-b343-4eba-955e-dd0c4970291a",
    "objectID": "b3a88880-b343-4eba-955e-dd0c4970291a"
  }

Here is the full body of JSON data - JSON data

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.