I'm getting a "ListError: list index out of range" in my "clean_json_response" function

563 Views Asked by At

I'm using a medium API to get a some information but after some API calls the python script ended with this error:

IndexError: list index out of range

Here is my Python code:

def get_post_responses(posts):
    #start = time.time()
    count = 0
    print('Retrieving the post responses...')
    responses = []
    for post in posts:
        url = MEDIUM + '/_/api/posts/' + post + '/responses'
        count = count + 1
        print("number of times api called",count)
        response = requests.get(url)
        response_dict = clean_json_response(response)
        responses += response_dict['payload']['value']
    #end = time.time()
    #four = end - start
    #global time_cal
    #time_cal.append(four)
    return responses

def check_if_high_recommends(response, recommend_min):
    if response['virtuals']['recommends'] >= recommend_min:
        return True

def check_if_recent(response):
    limit_date = datetime.now() - timedelta(days=360)
    creation_epoch_time = response['createdAt'] / 1000
    creation_date = datetime.fromtimestamp(creation_epoch_time)
    if creation_date >= limit_date:
        return True

It needs to work for more then 10000 followers for a user.

1

There are 1 best solutions below

0
On

i got an ans for my question... just i need to use try catch exception ...

  response_dict = clean_json_response(response)
        try:
            responses += response_dict['payload']['value']
        catch:
            continue