Hi Devs! In this lab we are going to automate some actions using Instagram API. Gain active followers - Algorithm Maybe some of you do not agree it is a good way to grow your IG page by using follow for follow method but after a lot of researching I found the proper way to use this method. I have done and used this strategy for a while and my page visits also followers started growing. The majority of people failing because they randomly targeting the followers and as a result, they are not coming back to your page. So, the key is to find people those have same interests with you. If you have a programming page go and search for IG pages which have big programming community and once you find one, don't send follow requests to followers of this page. Because some of them are not active even maybe fake accounts. So, in order to gain active followers, go the last post of this page and find people who liked the post. I think you get the idea. Those people will like your posts as well because they are active followers. Unofficial Instagram API In order to query data from Instagram I am going to use the very cool, yet unofficial, Instagram API written by . Pasha Lev Note: Before you test it make sure you verified your phone number in your IG account. The program works pretty well so far but in case of any problems I have to put disclaimer statement here: Disclaimer: This post published educational purposes only as well as to give general information about Instagram API. I am not responsible for any actions and you are taking your own risk. Let's start by installing and then logging in with API. pip install InstagramApi InstagramAPI InstagramAPI api = InstagramAPI( , ) api.login( from import "username" "password" Once you run the program you will see "Login success!" in your console. Get users from liked list We are going to search for some username (your target page) then get most recent post from this user. Then, get users who liked this post. Unfortunately, I can't find solution how to paginate users so right now it gets about last 500 user. users_list = [] api.login() api.searchUsername(username) result = api.LastJson username_id = result[ ][ ] user_posts = api.getUserFeed(username_id) result = api.LastJson media_id = result[ ][ ][ ] api.getMediaLikers(media_id) users = api.LastJson[ ] user users: users_list.append({ :user[ ], :user[ ]}) : def get_likes_list (username) 'user' 'pk' # Get user ID # Get user feed 'items' 0 'id' # Get most recent post # Get users who liked 'users' for in # Push users to list 'pk' 'pk' 'username' 'username' Follow Users Once we get the users list, it is time to follow these users. IMPORTANT NOTE: set time limit as much as you can to avoid automation detection. time sleep following_users = [] api.login() api.getSelfUsersFollowing() result = api.LastJson user result[ ]: following_users.append(user[ ]) user users_list: user[ ] following_users: print( + user[ ]) api.follow(user[ ]) sleep( ) : print( + user[ ]) sleep( ) from import : def follow_users (users_list) # Get users which you are following for in 'users' 'pk' for in if not 'pk' in # if new user is not in your following users 'Following @' 'username' 'pk' # after first test set this really long to avoid from suspension 20 else 'Already following @' 'username' 10 Unfollow Users This function will look users which you are following then it will check if this user follows you as well. If user not following you then you are unfollowing as well. follower_users = [] api.login() api.getSelfUserFollowers() result = api.LastJson user result[ ]: follower_users.append({ :user[ ], :user[ ]}) api.getSelfUsersFollowing() result = api.LastJson user result[ ]: following_users.append({ :user[ ], :user[ ]}) user following_users: user[ ] follower_users: print( + user[ ]) api.unfollow(user[ ]) sleep( ) : def unfollow_users () # Get your followers for in 'users' 'pk' 'pk' 'username' 'username' # Get users which you are following for in 'users' 'pk' 'pk' 'username' 'username' for in if not 'pk' in # if the user not follows you 'Unfollowing @' 'username' 'pk' # set this really long to avoid from suspension 20 Full Code with extra functions Here is the full code of this automation: pprint time sleep InstagramAPI InstagramAPI pandas pd users_list = [] following_users = [] follower_users = [] self.api = InstagramAPI( , ) api = self.api api.login() api.searchUsername(username) result = api.LastJson username_id = result[ ][ ] user_posts = api.getUserFeed(username_id) result = api.LastJson media_id = result[ ][ ][ ] api.getMediaLikers(media_id) users = api.LastJson[ ] user users: users_list.append({ :user[ ], :user[ ]}) bot.follow_users(users_list) api = self.api api.login() api.getSelfUsersFollowing() result = api.LastJson user result[ ]: following_users.append(user[ ]) user users_list: user[ ] following_users: print( + user[ ]) api.follow(user[ ]) sleep( ) : print( + user[ ]) sleep( ) api = self.api api.login() api.getSelfUserFollowers() result = api.LastJson user result[ ]: follower_users.append({ :user[ ], :user[ ]}) api.getSelfUsersFollowing() result = api.LastJson user result[ ]: following_users.append({ :user[ ], :user[ ]}) user following_users: user[ ] follower_users: print( + user[ ]) api.unfollow(user[ ]) sleep( ) bot = InstaBot() bot.get_likes_list( ) import from import from import import as : class InstaBot : def __init__ (self) "your_username" "your_password" : def get_likes_list (self,username) #Gets most recent post from user 'user' 'pk' 'items' 0 'id' 'users' for in 'pk' 'pk' 'username' 'username' : def follow_users (self,users_list) for in 'users' 'pk' for in if not 'pk' in 'Following @' 'username' 'pk' # set this really long to avoid from suspension 20 else 'Already following @' 'username' 10 : def unfollow_users (self) for in 'users' 'pk' 'pk' 'username' 'username' for in 'users' 'pk' 'pk' 'username' 'username' for in if not 'pk' in 'Unfollowing @' 'username' 'pk' # set this really long to avoid from suspension 20 # To follow users run the function below # change the username ('instagram') to your target username 'instagram' # To unfollow users uncomment and run the function below # bot.unfollow_users() it will look like this: some extra functions to play with API: api.login() api.getSelfUsernameInfo() result = api.LastJson username = result[ ][ ] full_name = result[ ][ ] profile_pic_url = result[ ][ ] followers = result[ ][ ] following = result[ ][ ] media_count = result[ ][ ] df_profile = pd.DataFrame( { :username, : full_name, :profile_pic_url, :followers, :following, : media_count, }, index=[ ]) df_profile.to_csv( , sep= , encoding= ) image_urls = [] api.login() api.getSelfUserFeed() result = api.LastJson result.keys(): item result[ ][ : ]: item.keys(): image_url = item[ ][ ][ ][ ] image_urls.append(image_url) df_feed = pd.DataFrame({ :image_urls }) df_feed.to_csv( , sep= , encoding= ) : def get_my_profile_details () 'user' 'username' 'user' 'full_name' 'user' 'profile_pic_url' 'user' 'follower_count' 'user' 'following_count' 'user' 'media_count' 'username' 'full name' 'profile picture URL' 'followers' 'following' 'media count' 0 'profile.csv' '\t' 'utf-8' : def get_my_feed () # formatted_json_str = pprint.pformat(result) # print(formatted_json_str) if 'items' in for in 'items' 0 5 if 'image_versions2' in 'image_versions2' 'candidates' 1 'url' 'image URL' 'feed.csv' '\t' 'utf-8' Mission Accomplished! I hope you learned something today and if you want to watch video tutorial of this post visit . Also check web application of Stay Connected! YouTube channel - Reverse Python Reverse Python