Open In App
Related Articles

Instagram Bot using Python and InstaPy

Improve Article
Save Article
Like Article

In this article, we will design a simple fun project “Instagram Bot” using Python and InstaPy. As beginners want to do some extra and learning small projects so that it will help in building big future projects. Now, this is the time to learn some new projects and a better future.

This python project gives the functionality of Instagram bot to like, comment, and follow profiles with particular hashtags on their posts. To do this we have to use InstaPy. Make sure you also install the Firefox browser since the latest version of InstaPy dropped support for Chrome. For installing InstaPy we use the command given below:

pip install instapy==0.6.8

The latest version is 0.6.9, but it crashes anytime, try to use comments. Works perfectly without the comment scripts.  


First, let us create a Python file and put the following code in it and Replace the username and password with yours, run the script, and this must get you inside Instagram.  


from instapy import InstaPy
session = InstaPy(username="your username",password="your password")

InstaPy does some other things, such as checking your internet connection and the status of the Instagram servers. We can observe this directly on the browser or in the logs:

It can take some time to load as you can see below

Like by tags

First, we can like some posts that are tagged #dance or #mercedes using like_by_tags():


session.like_by_tags(["dance", "mercedes"], amount=10, interact=True)

Here, we gave the method a list of tags to like and the number of posts to like for each given tag. Here, we instructed it to like ten posts each. InstaPy logs every action it takes.  

It mentions which post it liked and its link, description, location, and whether the bot commented on the post or followed the author.

Don’t like

We can use set_dont_like(): to prevent the bot to like inappropriate posts.


session.set_dont_like(["naked", "murder", "nsfw"])

Before running the code, we have to change a bit of code in the file present in ‘site-packages/instapy/ because Instagram has modified the HTML  


xpath[“like_image”] = {

   “like”: “//section/span/button[*[local-name () =’svg’]/@aria-label=’Like’]”,

   “unlike”: “//section/span/button[*[local-name () =’svg’]/@aria-label=’Unlike’]”,


Replace with:

xpath[“like_image”] = {

   “like”: “//section/span/button/div[*[local-name()=’svg’]/@aria-label=’Like’]”,

   “unlike”: “//section/span/button/div[*[local-name()=’svg’]/@aria-label=’Unlike’]”,


If we do not replace the above code, it throws the error of instapy: “Invalid Like Element!”

Set comments

Next, you can also leave some comments on the posts. First, enable commenting with set_do_comment(). Second, tell the bot what comments to leave with set_comments().


session.set_do_comment(True, percentage=100)
session.set_comments(["Nice", "Amazing", "Super"])

Set follow

Next, you can tell the bot to not only like the posts but also to follow some authors of those posts. You can do that with set_do_follow ().


session.set_do_follow(enabled=True, percentage=100)

Set interaction

After this, you have to use set_user_interact () to reflect the actual user experience after one interaction with the user interface. Here amount is the number of posts the bot will interact in a single profile.


session.set_user_interact(amount=1, randomize=True, percentage=100)

IMPORTANT: You must set configuration BEFORE call activity and also set interaction, which means after the above session settings keep the activities otherwise the bot will only like the posts but it won’t comment or follow.


Now that you’re done with the basic settings, it’s a good idea to end the session with the end().



Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 24 Jan, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials