Introduction:
I'm currently working with JSON and Python, building a web-scraper and anayisig the data. However sometimes the data that I want to scrape does not always get returned due to the IP getting blocked or rerouted or any of the other troubles one can encounter when scraping. So I want to check if the object in the JSON file exists before further processing.
Goal:
To catch if the object was/wasn't grabbed by the scraper using an if statement and searching the JSON file.
What I Have Tried:
- Using
if data["caption"] in data:
, - Using a for loop for the data,
- Calling the object directly as
if data["caption"]:
andif "caption":
, - For the loop;
for s in range(len(data)):
andif data[s]["caption"]:
Errors:
KeyError: 'caption'
TypeError: list indices must be integers or slices, not str
Code:
[{
"caption": "This is water.",
"images": [],
"timestamp": "2022-02-08T03:44:20.000Z",
},
{
"caption": "old man and the sea",
"images": [],
"timestamp": "2022-01-12T17:58:24.000Z",
},
{
"caption": "from last night",
"images": [],
"timestamp": "2021-12-02T15:52:26.000Z",
}]
import json
with open('result.json') as f:
data = json.loads(f.read())
for s in range(len(data)):
if data[s]["caption"]:
print("Caption is here")
else:
print("Caption is not here")
Currently the code will only work if the object 'caption' is in the code, however shows KeyError: 'caption'
when it hits a part of the file which does not have 'caption' in it. Any help would be greatly appreciated, thanks!