Home > Mobile >  I'm receiving a key error and unsure where the mistake is
I'm receiving a key error and unsure where the mistake is

Time:06-19

import json
import requests
from bs4 import BeautifulSoup


api_url = "https://mms.kcbs.us/members/evr_search_ol_json.php"

params = {
    "otype": "TEXT",
    "evr_map_type": "2",
    "org_id": "KCBA",
    "evr_begin": "6/16/2022",
    "evr_end": "12/31/2022",
    "evr_address": "",
    "evr_radius": "50",
    "evr_type": "269",
    "evr_openings": "0",
    "evr_region": "",
    "evr_region_type": "1",
    "evr_judge": "0",
    "evr_keyword": "",
    "evr_rep_name": "",
}

soup = BeautifulSoup(
    requests.get(api_url, params=params).content, features="lxml"
)


data = {
    0: {0: "title", 1: "dates", 2: "city/state", 3: "country"},
    1: {0: "event", 1: "reps", 2: "prize"},
    2: {0: "results"},
}

all_data = []
for element in soup.find_all("div", class_="row"):
    event = {}
    for i, col in enumerate(element.find_all("div", class_="col- 
      md-4")):
        for j, item in enumerate(col.strings):
            event[data[i][j]] = item
    all_data.append(event)

print(json.dumps(all_data, indent=4))

Im unsure where the key error would come from. it ran fine yesterday and i changed nothing. the error traceback is below line 41, in event[data[i][j]] = item KeyError: 1

CodePudding user response:

The error occurs when i=2 and j>=1. Following this API you should have something like

data = {
...
2: {0: "results", 1: "", 2: "", 3: ""},
...
}

CodePudding user response:

You get this error because in your variable data[2] the key 1 and 2 are missing

import json
import requests
from bs4 import BeautifulSoup

api_url = "https://mms.kcbs.us/members/evr_search_ol_json.php"
params = {"otype": "TEXT",    "evr_map_type": "2",    "org_id": "KCBA",
          "evr_begin": "6/16/2022",    "evr_end":    "12/31/2022",    "evr_address": "",
          "evr_radius": "50",       "evr_type": "269",    "evr_openings": "0",    "evr_region":
          "",       "evr_region_type": "1",    "evr_judge": "0",    "evr_keyword": "",
              "evr_rep_name": "", }
soup = BeautifulSoup(requests.get(api_url, params=params).content,
                     features="lxml")

data = {0: {0: "title", 1: "dates", 2: "city/state", 3: "country"},    1:
        {0: "event", 1: "reps", 2: "prize"},    2: {0:    "results"}, }
all_data = []
for element in soup.find_all("div", class_="row"):
    event = {}
    for i, col in enumerate(element.find_all("div", class_="col-md-4")):
        for j, item in enumerate(col.strings):
            try:
                event[data[i][j]] = item
            except KeyError:
                pass
            all_data.append(event)
print(json.dumps(all_data, indent=4))
  • Related