KeyError:instagram脚本中的“数据”错误,低位数没有问题,但100-150以上的数字没有问题

时间:2018-08-05 07:36:55

标签: python json python-3.x instagram instagram-api

我有一个Instagram脚本,用于将关注者和取消关注者的列表生成为单独的excel文件。它适用于拥有50-60或更少的关注者和以下关注者的帐户,但是一旦我在拥有100个关注者的帐户中使用/跟随它,就会给我以下错误

  

回溯(最近一次通话最后一次):文件“ insta-unfollower.py”,行   229英寸       main()在main中的文件“ insta-unfollower.py”,第183行       followers_list = get_followers_list()在get_followers_list中的第140行的文件“ insta-unfollower.py”       边缘响应['data'] ['user'] ['edge_followed_by'] ['edges]]:KeyError:“ data”

这是功能

def get_followers_list():
    followers_list = []

    followers_post = {
        'query_id': 17851374694183129,
        'variables': {
            'id': session.cookies['ds_user_id'],
            'first': 60
        }
    }
    followers_post['variables'] = json.dumps(followers_post['variables'])
    response = session.get(query_route, params=followers_post)
    response = json.loads(response.text)

    for edge in response['data']['user']['edge_followed_by']['edges']:
        followers_list.append(edge['node'])

    while response['data']['user']['edge_followed_by']['page_info']['has_next_page']:
        time.sleep(random.randint(5, 15))

        followers_post = {
            'query_id': 17851374694183129,
            'variables': {
                'id': session.cookies['ds_user_id'],
                'first': 3,
                'after': response['data']['user']['edge_followed_by']['page_info']['end_cursor']
            }
        }
        followers_post['variables'] = json.dumps(followers_post['variables'])
        response = session.get(query_route, params=followers_post)
        response = json.loads(response.text)

        for edge in response['data']['user']['edge_followed_by']['edges']:
            followers_list.append(edge['node'])

    return followers_list

这是主要函数调用

def main():
    if not os.environ.get('USERNAME') or not os.environ.get('PASSWORD'):
        sys.exit('please provide USERNAME and PASSWORD environement variables. Abording...')

    is_logged = login()
    if is_logged == False:
        sys.exit('login failed, verify user/password combination')

    time.sleep(random.randint(1, 3))

    connected_user = get_user_profile(os.environ.get('USERNAME'))
    print('You\'re now logged as {}'.format(connected_user['username']))
    print('{} followers'.format(connected_user['edge_followed_by']['count']))
    print('{} following'.format(connected_user['edge_follow']['count']))

    time.sleep(random.randint(2, 6))

    print('building followers list...')
    followers_list = get_followers_list()
    print('found {} followers'.format(len(followers_list)))
    for i in followers_list:
        l1.append(i['username'])
        l2.append(i['full_name'])

        df = DataFrame({'User name': l1, 'Full Name': l2})
        df
        df.to_excel('followers.xlsx', sheet_name='sheet1', index=False)
    print ("input success")



    is_logged_out = logout()
    if is_logged_out:
        sys.exit(0)

1 个答案:

答案 0 :(得分:0)

文档表明该代码过去可能有效,但是由于提供者不再支持该端点而不再有效。考虑将这段代码移植到他们的新Graph API中。