Jul-27-2022, 03:43 PM
The first step is creating a dataframe of usernames and their corresponding User IDs. You also need to login to an Instagram account for authentication purposes.
So the end product would be list of all the followers from those username/userid combinations in the
from instagram_private_api import Client, ClientCompatPatch from operator import itemgetter import pandas as pd import json import requests user_name = '' password = '' api = Client(user_name, password) influencers = [['dcfoodcravings', '5577994064'], ['dcfoodlovers', '1902259438'], ['excellence.beauty.lounge', '2213626850'], ['foodzie', '1486757'], ['fiolamaredc', '1109579078']] influencers_df = pd.DataFrame(influencers, columns=['username', 'userId'])Once that's complete, we then move on to getting a list of their followers. This is where the issue is. You can see for the variables
following_username
, following_userid
, and get_followers
I am using **hard coded values.** But what I'm trying to do is run all the userId
's (along with their corresponding username
's in the influencers_df
dataframe in a loop with dynamic variables.So the end product would be list of all the followers from those username/userid combinations in the
influencers_df
dataframe. def followers_users(userid_instagram): followers = [] combined = [] results = api.user_followers(userid_instagram, rank_token=api.generate_uuid()) followers.extend(results.get('users', [])) next_max_id = results.get('next_max_id') while next_max_id: results = api.user_followers(userid_instagram, rank_token=api.generate_uuid(), max_id=next_max_id) followers.extend(results.get('users', [])) next_max_id = results.get('next_max_id') userid = [followers[i]['pk'] for i in range(0,len(followers))] full_names = [followers[i]['full_name'] for i in range(0,len(followers))] usernames = [followers[i]['username'] for i in range(0,len(followers))] profile_pic_url = [followers[i]['profile_pic_url'] for i in range(0,len(followers))] followers_text = ['follower' for i in range(0,len(followers))] following_username = ['dcfoodcravings' for i in range(0,len(followers))] following_userid = ['5577994064' for i in range(0,len(followers))] combined.extend([list(i) for i in zip(userid, full_names, usernames, profile_pic_url, followers_text, following_username, following_userid)]) combined = sorted(combined, key=itemgetter(2), reverse=False) return(combined) get_followers = followers_users('5577994064') followers = pd.DataFrame(get_followers, columns = ['userID' , 'Full Name', 'username', 'Profile Picture', 'Type', 'following_username', 'following_userid']) followers