Build an Instagram Bot with Python and InstaPy

Are you interested in working on a simple yet powerful Python project that showcases your programming skills? In this article, we will walk you through building an Instagram Bot using Python and InstaPy. This fun project is perfect for beginners who want to take their Python skills to the next level by working on practical real-world applications.

What Is InstaPy?

InstaPy is an open source tool that automates interactions on Instagram. You can use it to find posts, leave comments and follow profiles based on specific hashtags. If you’re planning to dive deeper into automation and bot development, this can be a great learning project.

Project Overview

In this project, you will create an Instagram bot that:

  • Login to your Instagram account.
  • Likes posts tagged with specific hashtags.
  • Leave a comment on the post. Track users based on interaction.
  • By the end of this project, you will have a working Instagram bot and a solid understanding of how automation works with Python.

Getting Started

You will need Python installed on your machine to get started. Additionally, you will use InstaPy, which requires a Firefox browser since the latest versions have dropped support for Chrome.

Install InstaPy

Open your terminal or command prompt and use the following command to install InstaPy.

Python
pip install instapy==0.6.8

Note: The latest version, 0.6.9, may crash due to issues, so it is recommended to stick with version 0.6.8.

Setting Up Your Bot

Create a Python file (e.g., instabot.py) and add the following code.

Python
from instapy import InstaPy

# Replace 'your_username' and 'your_password' with your Instagram credentials
session = InstaPy(username="your_username", password="your_password")
session.login()

This script takes you to Instagram. InstaPy handles connection analysis and writes session information directly to your terminal.

Adding Functionality

Now that your bot can log in, it’s time to add some sweetness.

Like Posts by Tags

You can configure your bot to like posts tagged with specific hashtags, such as #dance or #mercedes:

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

Now that your bot can log in, it’s time to add some sweetness.

Avoid Liking Inappropriate Content

To avoid liking posts with certain keywords (like #nsfw), use the set_dont_like() function:

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

Important Code Adjustment

Instagram frequently updates its HTML structure, which can break InstaPy’s functionality. To prevent the “Invalid Like Element!” error, update the xpath_compile.py file located in site-packages/instapy/ with the following code:

Replace:

Python
xpath["like_image"] = {
   "like": "//section/span/button[*[local-name () ='svg']/@aria-label='Like']",
   "unlike": "//section/span/button[*[local-name () ='svg']/@aria-label='Unlike']",
}

With:

Python
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']",
}

This small tweak ensures your bot runs smoothly.

Adding Comments

Want to leave comments on your favorite posts? Enable the first line:

Python
session.set_do_comment(True, percentage=100)

Next, determine what information your bot should leave:

Python
session.set_comments(["Nice", "Amazing", "Super"])

Following Users

You can also instruct the bot to follow the profiles of users who interact with them:

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

Simulating Human Interaction

To make the bot’s actions more human-like, use set_user_interact():

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

This item controls the amount of information to communicate for each user.

Finalizing the Bot

Once all the connections are made, remember to end the meeting successfully:

Python
session.end()

Conclusion

This Instagram Bot project is a great way to explore Python automation and gain practical experience with InstaPy. You can further enhance this functionality by adding more features such as scheduling posts, analyzing follower activity, or even automated direct messaging.

Share This Post:

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top