I build a scraper with Python. It works when I place an static URL, but I would like to make a for each URL loop with a JSON file.
This code returns an error, KeyError. I red online that this happens because its an object and not an array. I'm not sure how to fix this. Could someone point me in the right way or maybe even review the code? I placed some screenshots of the errors, the way i look up the JSON info and the way the JSON file is structured.
JSON structure:
from bs4 import BeautifulSoup
import requests
import json
with open("C:\data.json") as my_json:
json_dict = json.load(my_json)
for website in json_dict[0][0]:
print("About to scrape: ", website)
print('step 1')
#get url
page_link = website
print('step 2')
#open page
page_response = requests.get(page_link, timeout=1)
print('step 3')
#parse page
page_content = BeautifulSoup(page_response.content, "html.parser")
print('step 4')
#Find info
naam = page_content.find_all(class_='<random class>')[0].decode_contents()
print('step 5')
#Print
print(naam)