我的“ clean_json_response”函数中出现“ ListError:列表索引超出范围”

时间:2019-04-03 06:28:23

标签: python python-3.x indexoutofboundsexception medium.com

我正在使用中型API来获取一些信息,但是在某些API调用后,python脚本以此错误结束:

  

IndexError:列表索引超出范围

这是我的Python代码:

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

该用户需要为超过10000个关注者工作。

1 个答案:

答案 0 :(得分:0)

我的问题回答了... 只是我需要使用try catch异常...

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