Skip to content
Home » Blog » How To Build An Instagram Bot Using Python And InstaPy

How To Build An Instagram Bot Using Python And InstaPy

How To Build An Instagram Bot Using Python And InstaPy

Introduction

Creating an Instagram bot using Python and InstaPy can be an effective strategy for automating specific tasks on the platform, saving you time and effort. InstaPy, a widely-used automation tool for Instagram, provides a strong framework for interacting with the platform programmatically.

let’s see what is InstaPy? InstaPy is a Python library that allows you to automate interactions with Instagram. It provides a simple and easy-to-use interface for performing actions such as liking, following, and commenting on posts. With InstaPy, you can create complex automation scripts to mimic human behavior and grow your Instagram account organically.

In this guide, we’ll walk through the process of setting up and using an Instagram bot with InstaPy, covering each step in detail to ensure you can use this powerful tool effectively and responsibly. From installing the necessary dependencies to configuring your bot’s behavior, you’ll learn how to tackle the full potential of InstaPy to up to date your Instagram activities and achieve your goals more efficiently.

Why Use an Instagram Bot?

Instagram is a powerful platform for businesses, influencers, and content creators. However, growing your following and maintaining an active presence can be time-consuming. Herein lies the value of automation. By automating tasks such as liking posts, following users, and commenting, you can save time and energy while still engaging with your audience effectively.

Prerequisites

Before entering into setting up your Instagram bot with InstaPy, there are a few prerequisites you need to have in place:

Python

First and foremost, ensure that Python is installed on your system. You can download and install Python from the official Python website (https://www.python.org/). Make sure to install a version compatible with InstaPy.

InstaPy

Install InstaPy using pip, which is Python’s package installer. You can do this by running the following command in your terminal or command prompt:


pip install instapy
    

WebDriver

InstaPy requires a web driver to interact with Instagram. A web driver essentially acts as a bridge between InstaPy’s Python code and the web browser. Typically, ChromeDriver is used as the web driver for InstaPy. You’ll need to download the appropriate ChromeDriver version for your operating system from the official ChromeDriver website (https://sites.google.com/a/chromium.org/chromedriver/downloads) and ensure it is installed on your system.

Once downloaded, make sure to add the ChromeDriver executable to your system’s PATH so that InstaPy can locate and use it.

Additionally, ensure that your web browser (such as Google Chrome) is installed and up-to-date, as InstaPy will utilize this browser through the ChromeDriver for its automation tasks.

By fulfilling these prerequisites, you’ll have the necessary foundation to proceed with setting up and using your Instagram bot with InstaPy effectively.

The infographic illustrates the step-by-step process of setting up an Instagram bot using InstaPy: Prerequisites: Ensure Python is installed, Install InstaPy via pip, Download and configure WebDriver (ChromeDriver). Step 1: Install InstaPy: Open terminal/command prompt, Run "pip install instapy", Verify installation with "instapy --version". Step 2: Download WebDriver: Download ChromeDriver, Add to system's PATH. Step 3: Create the Bot Script: Import InstaPy and Smart Run, Define login credentials, Create InstaPy session, Use smart_run context manager, Set general settings, activity settings, etc., Run the script with Python. Running Your Bot: Execute your automation script with Python. Security Note: Keep credentials safe, Avoid embedding credentials in the script. Advanced Configuration: Explore more features and configurations in InstaPy documentation. Conclusion: Building an Instagram bot with Python and InstaPy saves time and grows your account organically, Use responsibly to avoid violating Instagram's terms of service.
Boost your Instagram growth effortlessly! Follow these simple steps to set up an InstaPy bot and automate your engagement.

Step 1: Install InstaPy

To start your journey with InstaPy, you’ll first need to install it on your system. Follow these simple steps:

Open your Terminal or Command Prompt

Launch your terminal or command prompt application on your computer. This is where you’ll input commands to install InstaPy.

Run the Installation Command

Type the following command


pip install instapy
    

This command tells pip, Python’s package installer, to download and install InstaPy along with its dependencies. Pip will handle the installation process automatically, fetching the necessary files from the Python Package Index (PyPI).

Verify Installation

Once the installation is finished, you can verify that InstaPy was installed correctly by checking its version. Simply type the following command and press Enter:


instapy --version
    

If InstaPy is installed correctly, you should see its version number displayed in the terminal or command prompt window.

By following these steps, you’ll have successfully installed InstaPy on your system. Now, you’re ready to move on to the next steps and configure your bot to start automating tasks on Instagram.

Step 2: Download WebDriver

As I said earlier, You must Download the ChromeDriver from here and place it in a directory that is included in your system’s PATH, or specify the path to the driver in your script.

Step 3: Create the Bot Script

Here’s a more advanced version of the InstaPy Instagram bot script, offering extra features like engaging with users’ followers, targeting specific users, managing quotas, watching stories, and more. Let’s break down the script step-by-step to understand how our Instagram bot works.

Import InstaPy and Smart Run

The code starts by importing the InstaPy class from the instapy module, as well as the smart_run function from the instapy.util module. This prepares the environment to utilize InstaPy for Instagram automation.


from instapy import InstaPy
from instapy.util import smart_run
    

Define Login Credentials

Next, the script defines the Instagram username and password. You need to replace 'your_username' and 'your_password' with your actual Instagram credentials.


username = 'your_username'
password = 'your_password'
    

Create InstaPy Session

The code initializes an instance of the InstaPy class using the given username, password, and the headless_browser=True parameter. By setting headless_browser to True, the automation process operates without displaying the browser window, allowing it to run safely in the background.


session = InstaPy(username=username, password=password, headless_browser=True)
    

Must Read


Use Smart Run Context Manager

The “with smart_run(session):” statement indicates the beginning of a context-managed block where InstaPy executes its actions. This context manager handles the session in a smart way, ensuring proper setup, execution, and cleanup.


with smart_run(session):
    

Set General Settings

The function “set_relationship_bounds” establishes parameters for relationships with other users such as follower and following counts.

Similarly, “set_skip_users” specifies conditions to avoid certain types of users, such as private accounts or those without profile pictures.


session.set_relationship_bounds(enabled=True,
                                    delimit_by_numbers=True,
                                    max_followers=7500,
                                    min_followers=50,
                                    min_following=50,
                                    potency_ratio=-1.2,
                                    max_posts=1000)
    session.set_skip_users(skip_private=True,
                           skip_no_profile_pic=True,
                           skip_business=True,
                           skip_business_categories=['Creators & Celebrities', 'Food & Personal Goods'])
    

Set Activity Settings

“set_user_interact” Configures interaction settings with other users, like commenting or following.

Meanwhile, “like_by_tags” and “like_by_locations” sets up procedures for liking posts associated with particular hashtags or locations.


session.set_user_interact(amount=5, randomize=True, percentage=50, media='Photo')
    session.like_by_tags(['nature', 'travel', 'photography', 'tech', 'fitness'], amount=20, interact=True)
    session.like_by_locations(['213385402', '224442573'], amount=20)  # Example location IDs
    

Perform Specific User Interactions

“interact_user_followers” and “interact_user_likers” engage with followers or individuals who have liked posts from specific users.


session.interact_user_followers(['natgeo', 'nasa'], amount=10, randomize=True)
    session.interact_user_likers(['natgeo', 'nasa'], photos_grab_amount=3, follow_likers_per_photo=2, randomize=True)
        

Advanced Comment Settings

Adjusts settings related to posting comments on content.


session.set_do_comment(enabled=True, percentage=30)
    session.set_comments(['Nice shot!', 'Love this!', 'Amazing!', 'Beautiful picture!', 'Incredible!'])
    

Follow Based on Activity

Defines conditions for following users based on their activity, such as liking posts from specific users.


session.set_do_follow(enabled=True, percentage=50, times=1)
    session.follow_likers(['natgeo', 'nasa'], photos_grab_amount=5, follow_likers_per_photo=3, randomize=True, sleep_delay=600)
    

Unfollow Users

You can sets up unfollowing conditions for non-followers or all followed accounts.


 session.unfollow_users(amount=50, nonFollowers=True, style="RANDOM", unfollow_after=24*60*60, sleep_delay=600)
    session.unfollow_users(amount=50, allFollowing=True, style="FIFO", unfollow_after=72*60*60, sleep_delay=600)
    

Story Watching

Configures actions related to watching stories.


session.story_by_tags(['nature', 'travel'], amount=5)
    session.story_by_users(['natgeo', 'nasa'], amount=3)
    

Direct Messages

Sets up settings for sending direct messages to specific users.


session.set_do_reply_to_dms(enabled=True, percentage=50)
    session.set_do_send_dms(enabled=True)
    session.send_message(['user1', 'user2'], 'Hey! Check out our latest posts and let us know what you think!')
    

Engagement Settings

Configures settings related to engagement with other users’ posts.


session.set_dont_unfollow_active_users(enabled=True, posts=5)
    session.set_mandatory_language(enabled=True, character_set=['LATIN'])
    

Quota Supervisor

Sets limits and rules for the frequency of actions to avoid exceeding Instagram’s usage limits.


session.set_quota_supervisor(enabled=True,
                                 sleep_after=["likes", "comments_d", "follows", "unfollows", "server_calls_h"],
                                 sleepyhead=True,
                                 stochastic_flow=True,
                                 notify_me=True,
                                 peak_likes_hourly=40,
                                 peak_likes_daily=400,
                                 peak_comments_hourly=20,
                                 peak_comments_daily=200,
                                 peak_follows_hourly=30,
                                 peak_follows_daily=300,
                                 peak_unfollows_hourly=30,
                                 peak_unfollows_daily=300,
                                 peak_server_calls_hourly=200,
                                 peak_server_calls_daily=2000)

    

Specific User Blacklist

Specifies a blacklist of users to avoid interacting with.


 session.set_blacklist(enabled=True, campaign='general')
    

Advanced Settings

Configures additional delays and randomization for actions.


session.set_action_delays(enabled=True, like=5, comment=10, follow=5, unfollow=8, story=3, randomize=True, random_range_from=70, random_range_to=140)
    

Restrict Activity

Further restricts activity based on user profiles.


session.set_skip_users(skip_private=True, skip_no_profile_pic=True, skip_business=True)

    # End Session
    # Terminates the InstaPy session.
    session.end()
    

This integration provides a detailed explanation of each step in the code, making it easier to understand what each part does and how it contributes to the overall functionality of the Instagram bot.

Running Your Bot

After you’ve written your automation script, you can run it using Python.


python your_script.py

    

Your bot will then start interacting with Instagram according to the actions you specified in your script. You can run your bot on a schedule using cron jobs or a similar task scheduler to automate the process even further.

Security Note

Keep Credentials Safe: Avoid embedding your credentials directly within the script. Instead, Consider using environment variables or a configuration file to store sensitive information.

Infographic titled 'Optimizing InstaPy for Best Results' showing key steps: setting up InstaPy, choosing target audience, customizing engagement settings, scheduling activity, monitoring performance, and adjusting strategies for improved outcomes.
Maximize your Instagram automation with InstaPy! Follow these steps to optimize your bot for the best engagement and growth results.

Advanced Configuration

InstaPy offers many more features and configurations. Refer to the InstaPy documentation for more advanced usage and customization.

By following these steps, you can set up a basic Instagram bot that likes, comments, follows, and unfollows users based on your specified criteria. Always use automation tools responsibly to avoid violating Instagram’s terms of service.

Conclusion

Building an Instagram bot using Python and InstaPy is a powerful way to automate repetitive tasks and grow your Instagram account organically. With InstaPy’s simple interface and powerful features, you can create complex automation scripts that mimic human behavior and engage with your audience effectively. So why spend hours manually managing your Instagram account when you can let a bot do it for you? Give InstaPy a try and watch your Instagram presence!.

Source Code for Instagram bot


from instapy import InstaPy
from instapy.util import smart_run

# Login credentials
username = 'your_username'
password = 'your_password'

# Create an InstaPy session
session = InstaPy(username=username, password=password, headless_browser=True)

with smart_run(session):
    # General settings
    session.set_relationship_bounds(enabled=True,
                                    delimit_by_numbers=True,
                                    max_followers=7500,
                                    min_followers=50,
                                    min_following=50,
                                    potency_ratio=-1.2,
                                    max_posts=1000)
    session.set_skip_users(skip_private=True,
                           skip_no_profile_pic=True,
                           skip_business=True,
                           skip_business_categories=['Creators & Celebrities', 'Food & Personal Goods'])

    # Activity settings
    session.set_user_interact(amount=5, randomize=True, percentage=50, media='Photo')
    
    # Liking based on tags
    session.like_by_tags(['nature', 'travel', 'photography', 'tech', 'fitness'], amount=20, interact=True)

    # Liking based on location
    session.like_by_locations(['213385402', '224442573'], amount=20)  # Example location IDs

    # Specific user interactions
    session.interact_user_followers(['natgeo', 'nasa'], amount=10, randomize=True)
    session.interact_user_likers(['natgeo', 'nasa'], photos_grab_amount=3, follow_likers_per_photo=2, randomize=True)
    
    # Advanced comment settings
    session.set_do_comment(enabled=True, percentage=30)
    session.set_comments(['Nice shot!', 'Love this!', 'Amazing!', 'Beautiful picture!', 'Incredible!'])

    # Follow based on activity
    session.set_do_follow(enabled=True, percentage=50, times=1)
    session.follow_likers(['natgeo', 'nasa'], photos_grab_amount=5, follow_likers_per_photo=3, randomize=True, sleep_delay=600)

    # Unfollow users
    session.unfollow_users(amount=50, nonFollowers=True, style="RANDOM", unfollow_after=24*60*60, sleep_delay=600)
    session.unfollow_users(amount=50, allFollowing=True, style="FIFO", unfollow_after=72*60*60, sleep_delay=600)

    # Story watching
    session.story_by_tags(['nature', 'travel'], amount=5)
    session.story_by_users(['natgeo', 'nasa'], amount=3)

    # Direct messages
    session.set_do_reply_to_dms(enabled=True, percentage=50)
    session.set_do_send_dms(enabled=True)
    session.send_message(['user1', 'user2'], 'Hey! Check out our latest posts and let us know what you think!')

    # Engagement settings
    session.set_dont_unfollow_active_users(enabled=True, posts=5)
    session.set_mandatory_language(enabled=True, character_set=['LATIN'])

    # Quota supervisor
    session.set_quota_supervisor(enabled=True,
                                 sleep_after=["likes", "comments_d", "follows", "unfollows", "server_calls_h"],
                                 sleepyhead=True,
                                 stochastic_flow=True,
                                 notify_me=True,
                                 peak_likes_hourly=40,
                                 peak_likes_daily=400,
                                 peak_comments_hourly=20,
                                 peak_comments_daily=200,
                                 peak_follows_hourly=30,
                                 peak_follows_daily=300,
                                 peak_unfollows_hourly=30,
                                 peak_unfollows_daily=300,
                                 peak_server_calls_hourly=200,
                                 peak_server_calls_daily=2000)

    # Specific user blacklist
    session.set_blacklist(enabled=True, campaign='general')

    # Advanced settings
    session.set_action_delays(enabled=True, like=5, comment=10, follow=5, unfollow=8, story=3, randomize=True, random_range_from=70, random_range_to=140)

    # Restrict activity
    session.set_skip_users(skip_private=True, skip_no_profile_pic=True, skip_business=True)

    # End session
    session.end()

    

External Resources

External Resources for Building an Instagram Bot Using Python and InstaPy:

  1. InstaPy Documentation: The official documentation for InstaPy is a comprehensive resource that provides detailed information about installation, usage, and advanced features. You can find it here.
  2. InstaPy GitHub Repository: The GitHub repository for InstaPy contains the source code, issue tracker, and discussions related to the project. You can explore it here.
  3. InstaPy Wiki: The InstaPy Wiki on GitHub offers additional guides, tips, and troubleshooting advice for using InstaPy effectively. You can access it here.
  4. Python.org: The official website for the Python programming language is a valuable resource for learning Python syntax, best practices, and libraries. You can find tutorials, documentation, and community forums here.
  5. Online Forums and Communities: Websites like Reddit, Discord, and specialized forums dedicated to Python and Instagram automation are excellent places to connect with like-minded individuals, ask questions, and share your experiences.

By leveraging these external resources, you can enhance your understanding of Python programming, InstaPy, and Instagram automation, enabling you to build more sophisticated and effective bots for growing your Instagram presence.

Frequently Asked Questions

FAQ Section
1. Is it safe to use InstaPy for automating Instagram tasks?
InstaPy strives to emulate human behavior and abide by Instagram’s terms of service to ensure safety and compliance. However, using any automation tool carries some risk, so it’s essential to use InstaPy responsibly and avoid aggressive or spammy behavior.
2. Can I get banned from Instagram for using InstaPy?
While InstaPy aims to minimize the risk of being banned, there is still a possibility, especially if Instagram detects unusual or suspicious activity. It’s crucial to use InstaPy conservatively, follow Instagram’s guidelines, and avoid aggressive actions like mass following or excessive liking.
3. How often should I run my InstaPy bot?
The frequency of running your InstaPy bot depends on your account’s activity level, your growth goals, and Instagram’s limits on automation. It’s recommended to start with a conservative schedule, such as running the bot for a few hours each day, and monitor its performance closely to avoid triggering Instagram’s spam detection algorithms.
4. Can I customize the actions performed by my InstaPy bot?
Yes, InstaPy offers extensive customization options, allowing you to tailor the bot’s actions to your specific needs and preferences. You can specify parameters such as hashtags to like, users to follow, and comments to post, enabling you to create a personalized automation strategy for your Instagram account.
5. Does InstaPy support multiple Instagram accounts?
Yes, InstaPy supports managing multiple Instagram accounts within the same script, making it convenient for users with multiple profiles to automate their interactions. However, it’s essential to ensure that each account complies with Instagram’s terms of service and usage limits.
6. How can I troubleshoot issues with my InstaPy bot?
If you encounter any issues or errors while using InstaPy, you can refer to the official documentation, GitHub repository, or online community forums for assistance. Common troubleshooting steps include checking for updates, verifying your account credentials, and adjusting your bot’s settings to comply with Instagram’s guidelines.

About The Author

2 thoughts on “How To Build An Instagram Bot Using Python And InstaPy”

  1. Pingback: How to Build Your own Advanced AI Writer - EmiTechLogic

  2. Pingback: Free Online MP4 to MP3 Converter with Python - EmiTechLogic

Leave a Reply

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