Instagram Bot using Python and InstaPy

24 Jan, 2021
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.  
Login
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.  
Python3
from instapy import InstaPy  session = InstaPy(username="your username",password="your password")session.login()
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():
Python3
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.
Python3
session.set_dont_like(["naked", "murder", "nsfw"])
Before running the code, we have to change a bit of code in the xpath_compile.py file present in ‘site-packages/instapy/xpath_compile.py because Instagram has modified the HTML  
Remove:
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().
Python3
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 ().
Python3
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.
Python3
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.
End
Now that you’re done with the basic settings, it’s a good idea to end the session with the end().
Python3
session.end()
Python-projects
python-utility
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.