-3

我正在使用中等 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 个关注者。

4

1 回答 1

0

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

  response_dict = clean_json_response(response)
        try:
            responses += response_dict['payload']['value']
        catch:
            continue
于 2019-05-02T12:09:21.063 回答