Let's see how you can use some fairly simple python code to pull various publicly available data from instagram.
While phasing from Employed to Self-Employed, I got immersed in my own projects that I've been wanting to do for a long time. After a couple of telegram bots with e-acquiring, I decided to try my luck with Instagram. As someone who had only worked with finished and cleaned up data before, I was interested in learning more about the data mining process.
Let's start with what libraries to use. Since I write in Python, I chose libraries for it.
Facebook has an official API for interacting with Instagram. They are the Graph API and the Instagram Basic Display API. I found the process of setting it up and using it overly complicated, so I decided to look for an easier solution.
Of the unofficial APIs, there is the relatively popular InstaPy (12k GitHub), which is based on Selenium. Such a framework seemed to me cumbersome.
After hours of searching my choice fell on a fairly convenient library instabot, the library itself, the documentation.
Before we start sorting out the code, a couple of remarks should be made. I should say right away that I'm quite skeptical about using such frameworks for automation of activity (likes, comments, subscriptions) in order to increase audience.
Instragram does not take well to the use of such libraries for promotion purposes, and in general, it treats "non-human" activity negatively. So I do not recommend using them on your main account. I don't know what the probability of being blocked is, but it's obviously different from zero.
My main interest was to play around with the data.
For the purposes of this article, I'm going to talk about how you can get the following information:
It's much more interesting to look at this kind of information gathering process not as an isolated task, but as an applied task. So for each item, I've found some real-world challenges and shown how they can be solved.
Let's imagine a situation where you're a young blogger and you've decided to run a prank to expand your audience. New Year's Eve, all the more so soon, so the example is relevant. Let's assume that the main criterion of the prank is to be subscribed to you.
So we can formulate the task of how to randomly select one or more subscribers to give them gifts.
Let's see how this can be done. First, you need to log in. By the way, so as not to jeopardize my main account, 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)
Once we are logged in, we can get a list of subscribers and a list of subscriptions for any user with an open account. This is done as follows.
user_followers = bot.get_user_followers(username) user_following = bot.get_user_following(username)
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 the usernames of the users you need to do the following:
user_id = user_followers[i] username = bot.get_username_from_user_id(user_id)
However, keep in mind that the get_username_from_user_id query does not work instantaneously, so it is better to work with user_id inside the program and resolve it to username only if necessary.
To randomly select multiple username subscribers, you can, for example, do the following
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 conduct collective raffles – you can get lists of subscribers for several accounts and already choose winners among the many users who are subscribed to all the necessary profiles.
Keeping with the blogger theme, imagine that you're conducting a drawing not only among the users who are subscribed to you, but also among those who like your post. How would you get the list of users in that 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)
List of people who left a comment:
users_commented = bot.get_media_commenters(media_pk)
You can also get a list of comments under the post
comments = bot.get_media_comments(media_pk) # 20 last comments all_comments = bot.get_media_comments_all(media_pk) #all comments
From there you can work with these lists in the same way as in the previous point. For example, you can select winners among those users who are subscribed to you and who have liked and commented on the last N posts.