Alex
Alex

Reputation: 272

API Call using request module in python

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)

Upvotes: 0

Views: 130

Answers (1)

ahmedshahriar
ahmedshahriar

Reputation: 1076

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

Upvotes: 1

Related Questions