How to Make an Instagram Bot with Python

By Adrian Horning
Featured image

Instagram has become one of the most popular social media platforms, with millions of active users sharing photos, videos, and stories every day. For businesses and individuals looking to grow their presence on the platform, automation can be a powerful tool. In this guide, we'll walk through how to create an Instagram bot using Python to automate various activities like following users, liking posts, and leaving comments.

Setting Up Your Environment

Before we dive into coding our Instagram bot, we need to set up our development environment:

  1. Install Python: If you don't already have Python installed, download and install the latest version from python.org.
  2. Install required libraries: We'll be using the InstaPy library, which provides a high-level API for Instagram automation. Install it using pip:
    pip install instapy
  3. Install a web driver: InstaPy uses Selenium under the hood, which requires a web driver. The latest versions of InstaPy support Firefox, so make sure you have Firefox installed on your system[1].

Creating Your Instagram Bot

Now that we have our environment set up, let's create a basic Instagram bot:

Step 1: Importing Libraries and Logging In

First, we'll import the necessary modules and log in to Instagram:

from instapy import InstaPy

# Create an InstaPy session
session = InstaPy(username="your_username", password="your_password")
session.login()

Replace "your_username" and "your_password" with your actual Instagram credentials[1].

Step 2: Defining Bot Actions

Now we can define the actions our bot will take. Let's start with liking posts based on hashtags:

# Like posts with specific hashtags
session.like_by_tags(["bmw", "mercedes"], amount=5)

This code will like 5 posts for each of the specified hashtags[1].

Step 3: Following Users

We can also set up our bot to follow users based on certain criteria:

# Follow users who post about cars
session.set_do_follow(True, percentage=50)
session.set_user_interact(amount=3, randomize=True, percentage=100, media='Photo')
session.interact_user_followers(['bmw', 'mercedesbenz'], amount=10, randomize=True)

This code will follow 50% of the users it interacts with, interact with 3 of their posts, and target followers of the specified accounts[1].

Step 4: Commenting on Posts

To engage more deeply with content, we can set up our bot to leave comments:

# Set up comments
comments = ['Nice car!', 'Great photo!', 'Awesome!']
session.set_do_comment(True, percentage=25)
session.set_comments(comments)

This will leave comments on 25% of the posts it interacts with, using one of the predefined comments[1].

Step 5: Setting Limits

It's important to set limits to avoid overusing the bot and potentially getting flagged by Instagram:

# Set quotas
session.set_quota_supervisor(enabled=True, peak_comments_daily=240, peak_comments_hourly=21)

This sets a maximum of 240 comments per day and 21 comments per hour[1].

Step 6: Ending the Session

Finally, we need to end the session:

# End the session
session.end()

Putting It All Together

Here's what our complete Instagram bot script looks like:

from instapy import InstaPy

# Create an InstaPy session
session = InstaPy(username="your_username", password="your_password")
session.login()

# Like posts with specific hashtags
session.like_by_tags(["bmw", "mercedes"], amount=5)

# Follow users who post about cars
session.set_do_follow(True, percentage=50)
session.set_user_interact(amount=3, randomize=True, percentage=100, media='Photo')
session.interact_user_followers(['bmw', 'mercedesbenz'], amount=10, randomize=True)

# Set up comments
comments = ['Nice car!', 'Great photo!', 'Awesome!']
session.set_do_comment(True, percentage=25)
session.set_comments(comments)

# Set quotas
session.set_quota_supervisor(enabled=True, peak_comments_daily=240, peak_comments_hourly=21)

# End the session
session.end()

Advanced Features

Once you've mastered the basics, you can explore more advanced features of InstaPy:

Unfollowing Users

You can set up your bot to unfollow users who don't follow you back:

session.unfollow_users(amount=10, nonFollowers=True, style="FIFO", unfollow_after=3*60*60, sleep_delay=450)

This will unfollow 10 non-followers, waiting 3 hours before unfollowing and sleeping for 450 seconds between actions[1].

Interacting with Specific Users

You can target specific users to interact with:

session.interact_by_users(['user1', 'user2', 'user3'], amount=5, randomize=True)

This will interact with 5 random posts from each of the specified users[1].

Filtering Content

InstaPy allows you to filter the content you interact with:

session.set_relationship_bounds(enabled=True, potency_ratio=None, delimit_by_numbers=True, max_followers=5000, max_following=5555, min_followers=45, min_following=77)

This sets criteria for the types of accounts your bot will interact with, based on their follower and following counts[1].

Best Practices and Considerations

While Instagram bots can be powerful tools, it's important to use them responsibly:

  1. Respect Instagram's terms of service: Excessive automation can lead to account suspension.
  2. Use realistic interaction rates: Mimic human behavior by setting reasonable limits on actions per hour and day.
  3. Diversify your actions: Don't just like posts or follow users. Mix up your bot's activities to appear more natural.
  4. Monitor your bot: Regularly check on your bot's activities and adjust settings as needed.
  5. Use proxies: Consider using proxy servers to avoid IP bans, especially if running multiple bots.
  6. Keep your bot updated: Instagram frequently updates its platform, so make sure to keep your bot and its dependencies up to date.

Conclusion

Creating an Instagram bot with Python and InstaPy can be a powerful way to grow your Instagram presence. By automating likes, follows, and comments, you can increase your engagement and reach on the platform. However, it's crucial to use these tools responsibly and in compliance with Instagram's terms of service.

Remember, while automation can be helpful, genuine engagement and high-quality content are still the keys to long-term success on Instagram. Use your bot as a supplement to, not a replacement for, your personal efforts on the platform.

With the steps and code samples provided in this guide, you should now have a solid foundation for creating your own Instagram bot. Experiment with different settings and features to find what works best for your specific needs and goals.

Citations

  1. https://realpython.com/instagram-bot-python-instapy/
  2. https://www.geeksforgeeks.org/make-an-instagram-bot-with-python/
  3. https://www.geeksforgeeks.org/how-to-make-an-instagram-bot-with-python-and-instapy/
  4. https://www.youtube.com/watch?v=uyEIihDoXR8
  5. https://www.youtube.com/watch?v=cW7kMeOUr20
  6. https://www.youtube.com/watch?v=_odcrBrvxdY
  7. https://www.reddit.com/r/Python/comments/tre7to/i_made_a_webbased_instagram_bot_that_scrapes/
  8. https://www.reddit.com/r/learnpython/comments/13rq5xu/instagram_bot_using_python/
Scrape Creators
Copyright © . All rights reserved.
Resources
Blog
Contact

adrian@thewebscrapingguy.com

Twitter Instagram