Instagram Bot using Python and InstaPy.

Instagram Bot using Python and InstaPy.

This is a fun python project for a beginner like me which is making an Instagram bot to like, comment and follow profiles with particular hashtags on their posts.
I referred to an article of “Real Python” as though there were some major errors I encountered, which I solved through googling and on my own. Selenium and the Page Object Pattern, which together serve as the basis for InstaPy, are used here. Make sure you also install the Firefox browser since the latest version of InstaPy dropped support for Chrome . First, you must install InstaPy:
pip install instapy==0.6.8

The latest version is 0.6.9, but it crashes anytime I try to use comments. Works perfect without the comment scripts.
First, let us create a Python file and put the following code in it:

1.png

Replace the username and password with yours, run the script, and this must get you inside Instagram.
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

2.png

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

3.png

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.
We can use set_dont_like(): to prevent the bot to like inappropriate posts.

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!"

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 ():

4.png

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 ():

5.png

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.

6.png

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.

7.png

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

After much of hit and trial this method worked completely for me.
This is my first coding article.
Thankyou, Keep Hustling.

Did you find this article valuable?

Support Anirudh Panda by becoming a sponsor. Any amount is appreciated!