Automating the Instagram task has been something that most
people want to do. So here comes my another Python tutorial where we will Create Instagram Bot in Python 3 using Instabot Library.
I have seen quite a lot of tutorials online regarding the
creation of bot Instagram but I came across one that uses Selenium library.
Here your code will open a web page then browse through all the codes and kinds
of stuff. These bots are more like performing a Web
Scrapping using Python.
This tutorial is not that. In this tutorial, we will create a
real bot. No checking of Instagram’s page source code or any opening of the web
browser (like selenium or beautiful soup) does.
Installing Instagram Bot Library
In this tutorial I will be
using Python 3 programming language to create an Instagram bot.
We will start by importing our library module (instabot-py)
using PIP command
# For Mac and Linux Uses
pip3 install instabot-py
# For Windows Users
pip install instabot-py
The above command will install the module from PyPi repository.
If you want to use the
source and build from there, then you can install as follows:
pip3 install
git+https://github.com/instabot-py/instabot.py
What Can The Instagram Bot Do?
As mentioned in introduction unlike
the bots that uses Selenium or Beautiful Soup with logic of web scraping, this
would be a powerful bot that can:
- Login to your Instagram account
- View Followers and Account you are following
- Like Photo or Video
- Follow/Unfollow Account
- Make or See Comments
- And much more
See the library is really a powerful one. You can check
the instabot-py documentation for
more information.
Writing The Code
It sounds cool but let me not make it boring with lots of theoretical stuff. Let us jump into code it.
Before that, you will be surprised
that we can write this bot with just FOUR lines of Code.
Login To Instagram
We will start by importing the
necessary file and performing the login.
from instabot import Bot
bot = Bot()
bot.login(username="instacodeblog",
password="MY_SECTRET_PASSWORD_HERE")
If you run the code, you
should be able to see the output in terminal as follows:
2021-01-13 22:17:14,366 - INFO - Instabot version:
0.117.0 Started
2021-01-13 22:17:14,367 - INFO - Recovery from
<PATH_TO_YOUR_PROJECT/config/instacodeblog_uuid_and_cookie.json: COOKIE True
- UUIDs True - TIMING, DEVICE and ...
- user-agent=Instagram 117.0.0.28.xxx Android
(28/9.0; 420dpi; 1080x1920; OnePlus; ONEPLUS A3003; OnePlus3; qcom; en_US;
180xxx800)
- phone_id=8f9xx80d-b0de-4ec1-9590-6280xxx34cf7
- uuid=22b5xx51-96bb-43b4-8ad1-b20c89dfxxxe
-
client_session_id=79bxx5b2-c73d-4fe7-b5eb-f05c7xxxxx8c
- device_id=android-285xx1afebxxxbba
2021-01-13 22:17:14,368 - INFO - LOGIN FLOW! Just
logged-in: False
2021-01-13 22:17:17,656 - INFO - Logged-in
successfully as 'instacodeblog'!
2021-01-13 22:17:17,658 - INFO - Total requests:
49
The output log has lots of
stuff right. So basically the bot mimics a real device and performs the login.
You can see that our login was a successful one.
You will also find one
directory/folder created called config. It will have all the log information
there. It will help you debug the process.
Getting Instagram Followers
Now let us modify the above
code to get your list of followers. Well all you need to do is add a line of
code there:
from instabot import Bot
bot = Bot()
bot.login(username="instacodeblog", password="MY_SECTRET_PASSWORD_HERE")
bot.get_user_followers('instacodeblog')
Now you can run the code,
and the output will be as follows:
2021-01-13 22:17:17,366 - INFO - Instabot version:
0.117.0 Started
2021-01-13 22:17:17,367 - INFO - Recovery from
<PATH_TO_YOUR_PROJECT/config/instacodeblog_uuid_and_cookie.json: COOKIE True
- UUIDs True - TIMING, DEVICE and ...
- user-agent=Instagram 117.0.0.28.xxx Android
(28/9.0; 420dpi; 1080x1920; OnePlus; ONEPLUS A3003; OnePlus3; qcom; en_US;
180xxx800)
- phone_id=8f9xx80d-b0de-4ec1-9590-6280xxx34cf7
- uuid=22b5xx51-96bb-43b4-8ad1-b20c89dfxxxe
-
client_session_id=79bxx5b2-c73d-4fe7-b5eb-f05c7xxxxx8c
- device_id=android-285xx1afebxxxbba
2021-01-13 22:17:17,368 - INFO - LOGIN FLOW! Just
logged-in: False
2021-01-13 22:17:17,656 - INFO - Logged-in
successfully as 'instacodeblog'!
Getting followers of 45038783390: 100%|███| 29/29
[00:01<00:00, 28.00it/s]
2021-01-13 22:17:17,658 - INFO - Total requests:
49
Conclusion
We learned how to create a powerful, real and awesome Instagram bot in Python 3 using instabot-py library.
We have seen how we can log in, view followers and upload photo to Instagram
using the bot.
I hope
you have enjoyed this tutorial and having fun exploring more.