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
Comparing to the previous output you can find a new line. Which shows Getting followers with the ID of my Instagram account (instacodeblog).
With the output, I am sad, only 29 followers. Please follow my Instagram account. Next time I run this bot, hope I can see more followers.
https://instagram.com/instacodeblog/
I would like to thank you for showing your support.
Uploading Photo Using Instagram Bot
Let us move further and perform one more task. Well, let us make our bot upload photo along with some cool caption. We will also put a hashtag in our caption.
We will again modify above code by adding just one line.
This automation will be a real one. If you see my Instagram post, you will find this post, make sure to like, comment and share it. Well, you can create a bot to do that.
from instabot import Bot bot = Bot() bot.login(username="instacodeblog", password="MY_SECTRET_PASSWORD_HERE") bot.upload_photo( 'Instagram_bot.jpg', caption='Create Instagram Bot in Python 3 using instabot-py Library. \\n This Post is uploaded by the bot. You can check my blog for how to perform the task. \\n\\n #instacodeblog #instagrambot #instabot #bot #python #pythonproject #programming #fun')
Well you are done. That is all to upload the photo to your account using the bot. Now you can schedule and automate the task. It would be fun right.
the output would be as follows on your terminal
2021-01-13 22:56:22,366 - INFO - Instabot version: 0.117.0 Started 2021-01-13 22:56:22,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:56:22,368 - INFO - LOGIN FLOW! Just logged-in: False 2021-01-13 22:56:22,656 - INFO - Logged-in successfully as 'instacodeblog'! FOUND: w:717 h:717 r:1.0 2021-01-13 22:56:22,691 - INFO - Photo 'Instagram_bot.jpg' is uploaded. 2021-01-13 22:56:22,693 - INFO - Total requests: 63
Make sure that your image is square in size. Also in my case, the image is in the same directory as the script.
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. Also, do share this post and like my Instagram account (@instacodeblog).
Did you face any issues or have other awesome methods? Let me know in the comment section.