Let's see how, using a fairly simple python code, you can pull out various publicly available data from Instagram.
During the phase transition from Employed to Self-Employed, I immersed myself in my own projects that I had long wanted to do. After a couple of telegram bots with e-acquiring, I decided to try my luck with Instagram. As a person who previously worked only with ready and cleaned data, it was interesting for me to get to know the data mining process closer.

Let's start with which libraries to use. Since I write in python, I chose libraries for it.
Facebook has an official API for interacting with Instagram. These are the Graph API and the Instagram Basic Display API. The process of setting it up and using it seemed overly complicated to me, so I decided to look for a simpler solution.
Of the unofficial APIs, there is the relatively popular InstaPy (12k GitHub) based on Selenium. Such a framework seemed cumbersome to me.
After several hours of searching, my choice fell on the rather convenient instabot library, the library itself, and the documentation.
Before we start to deal with the code, it is worth making a couple of remarks. I’ll make a reservation right away that I’m quite skeptical about using such frameworks to automate activity (likes, comments, subscriptions) in order to increase the audience.
Instagram is not particularly good at using such libraries for promotion purposes, and in general, it has a negative attitude towards “non-human” activity. Therefore, I do not recommend using them on your main account. I don’t know what the probability is that you can be blocked, but it is clearly different from zero.
My main interest was to play around with the data.
What can be done?
In this article, I will talk about how you can get the following information:
- Subscriptions and followers of a specific account
- Users who liked / commented and stories anonymously
- Posts by a specific user
- User information
- Downloading images from Instagram
It is much more interesting to consider such a process of collecting information not as an isolated task, but as an applied task. Therefore, for each item, I found some real problems and showed how they can be solved.
List of subscribers
Let's imagine that you are a young blogger and you decide to hold a raffle to expand your audience. New Year, especially soon, so the example is relevant. Let's say that the main criterion for the draw is to be subscribed to you.
Thus, we can formulate a problem - how to randomly select one or more subscribers to give them gifts.
Let's see how this can be done. To get started, you need to log in. By the way, in order not to put my main account at risk, I created a new one and made all requests through it.
from instabot import Bot
bot = bot()
bot.login(username = INST_USERNAME, password = INST_PASSWORD)
After we are authorized - we can get the list of subscribers and the list of subscriptions for any user with an open account. This is done in the following way.
user_followers = bot.get_user_followers(username) # List of followers
user_following = bot.get_user_following(username) # List of subscriptions
It is worth noting that in this case we will see something like
['1537613519',
'7174630295',
'5480786626',
... ,
'6230009450',
'4294562266',
'27518898596']
This is the user_id of the users. In order to get usernames of users, you need to do the following:
user_id = user_followers[i]
username = bot.get_username_from_user_id(user_id)
However, it should be borne in mind that the get_username_from_user_id request does not work instantly, and inside the program it is better to work with user_id and resolve it to a username only if necessary.
user_followers = bot.get_user_followers(username)
amount = len(user_followers)
winners = np.random.choice(amount, N, replace=False)
winners_usernames = [bot.get_username_from_user_id(users_followers[i]) for i in winners]
Considering that bloggers like to hold collective drawings, you can get lists of subscribers for several accounts and choose the winners among the many users subscribed to all the necessary profiles.
List of people who liked a post
Continuing to exploit the blogging theme, let's imagine that you are holding a draw not only among users who subscribe to you, but also left a like under your post. How to get a list of users in this case?
First you need to get the media_pk from the link to your post:
media_link = 'https://www.instagram.com/p/CJQRFj4Jq1G/?utm_source=ig_web_copy_link'
media_pk = bot.get_media_id_from_link(media_link)
Then for the list of people who liked it:
users_liked = bot.get_media_likers(media_pk)
With every hour spent on social networks, we provide information companies with more and more information about ourselves. And the development of technologies for analyzing this data makes it possible not only to know something about us and our behavior, but also to predict our most likely actions, or even unobtrusively influence the process of performing these actions. To be honest, I've always been fascinated by the power that companies have, perhaps knowing more about us than we do.