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.
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
- AI Pulse Weekly: December 2024 – Latest AI Trends and Innovations
- Can Google’s Quantum Chip Willow Crack Bitcoin’s Encryption? Here’s the Truth
- How to Handle Missing Values in Data Science
- Top Data Science Skills You Must Master in 2025
- How to Automating Data Cleaning with PyCaret
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.
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:
- 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.
- 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.
- InstaPy Wiki: The InstaPy Wiki on GitHub offers additional guides, tips, and troubleshooting advice for using InstaPy effectively. You can access it here.
- 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.
- 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.
Pingback: How to Build Your own Advanced AI Writer - EmiTechLogic
Pingback: Free Online MP4 to MP3 Converter with Python - EmiTechLogic