Adventures in Machine Learning

Automate Your Instagram Presence with InstaPy and Python

Building an Instagram Bot with Python and InstaPy

In this modern era, social media has become a vital tool for reaching out to a wider audience. Instagram is one such platform where businesses and individuals share their stories, connect with people, and create an online presence.

However, with the vast number of users and their respective content, it can be daunting to manually manage and keep up with the engagement levels. This is where the automation script comes in handy, and InstaPy is one such tool that can help automate Instagram activities.

How Instagram Bots Work

An Instagram bot is a tool that automates consistent activity such as liking, commenting, or following on the platform. With the bot, users can post regularly while still engaging with their audience.

The automation script performs actions based on specific criteria set by the user, such as liking posts with specific hashtags or commenting on posts that mention a company name. To automate the Instagram actions, a browser automation tool such as Selenium and Firefox WebDriver can be used.

This browser automation tool can be used to login to Instagram and interact with the platform as a regular user would. The tool acts as a programmable web browser that can execute tasks such as clicking, typing, scrolling, and checking for elements on a webpage.

A popular pattern known as the Page Object Pattern is employed in building effective automation scripts. The Page Object pattern enables developers to group user interface (UI) elements that perform specific tasks while acting as a single component.

This approach helps to maintain the DRY principle, where the code is written once and reused in different parts of the script. In essence, the script separates the UI from the code and instead builds a class that represents a UI element.

Essential Features of InstaPy

Quota Supervisor

Instagram operation quotas limit the amount of engagement a bot can perform. With InstaPy’s

Quota Supervisor feature, users can set their engagement limits to keep them within Instagram’s terms of service, preventing a ban or penalty.

The feature allows users to delay actions after specific steps and pause the bot for a specific amount of time when the quota limit is reached.

Headless Browser

A headless browser is a browser without a graphical user interface (GUI). This feature allows for the performant automation of Instagram activities without opening a browser window.

The use of a headless browser reduces CPU consumption and can run on a server-side thus supporting massive operations.

Analyzing Posts with AI

InstaPy also incorporates Augmented Intelligence (AI) to analyze user’s posts and determine user interests, lifestyles, or other relevant information. This functionality is achieved with the use of a pre-built model called ClarifAI, an image recognition tool that processes images and returns relevant information such as objects, themes, or texts.

How Instagram Bots Work

Automation and Consistent Activity

An Instagram bot automates consistent activity such as liking, commenting, and following of posts. The purpose is to increase engagement levels while preventing the tedious manual activity.

Posting consistent content is one way of maintaining a strong presence on Instagram, however, bot automation allows a user to keep this presence active at all times without being online.

Instagram Developer API Limitations

Instagram has limitations on the utilization of their developer API. The API is typically restricted to the reading of data such as hashtags, comments, and user information.

Instagram does not allow third-party automation tools to log in and make changes to a user’s account. As a consequence, browser automation tools such as Selenium and Firefox WebDriver are employed to automate Instagram activities.

Using Browser Automation

To use browser automation, Selenium and Firefox WebDriver are crucial. Selenium is a browser automation tool that can emulate the actions performed by a human user.

The tool can perform actions such as blacklisting and whitelisting of keywords, following people who have followed competitors, and commenting on posts that include keywords specified by the user. Firefox WebDriver is an interface that interacts with the Firefox browser.

This tool makes it easy to automate Instagram activities by providing an API to interact with the browser and access its functionalities.


In conclusion, Instagram bot automation can be an effective tool for enhancing one’s presence on the platform. InstaPy is one such automation script that offers essential features such as the

Quota Supervisor,

Headless Browser, and

Analyzing Posts with AI. Selenium and Firefox WebDriver are vital tools for automation via browser automation.

Instagram API limitations restrict automation tools, hence the need for such automation tools. It is vital to note that while the use of bots can be strategic, it needs to be implemented with caution and transparency.

It is essential to understand Instagram’s terms of service and comply with the set rules and limitations.

How to Automate a Browser

Automating a browser is the process of programming a tool that interacts with web pages automatically. In the context of Instagram, automating the browser can be used to log in to Instagram, follow users, like posts, or perform other actions.

Selenium is a browser automation tool that can simplify the process of automating a web browser. Here are the steps to automate a browser using this tool.

Installing Selenium and WebDriver

Selenium is an open-source tool that can automate a browser while WebDriver is a component that allows Selenium to interact with a specific browser. Selenium WebDriver can automate the web browsers Chrome, Firefox, and many more.

To install Selenium, visit the Selenium website and download the Python bindings, then install it via pip. Once installed, download the WebDriver corresponding to your browser and add it to your system path.

Navigating to Instagram

The first step in automating your browser to perform an action on Instagram is visiting the site. This is done by navigating to the login page of the platform.

To navigate to the login page, create a WebDriver instance such as “driver = webdriver.Firefox()”, which initiates the Firefox browser, then navigate to the login page by using the “driver.get()” method. The “get()” method directs the browser to the targeted URL.

Entering Your Credentials

Once you arrive at the login page, enter your login credentials. This is done by automating the “username” and “password” fields with the keys using the “send_keys()” method of Selenium WebDriver.

After inputting the credentials, the “login button” can be clicked programmatically with the “click()” method. The bot now logs in to your account and is ready to perform actions on the platform.

How to Use the Page Object Pattern

The Page Object Pattern is an object-oriented design pattern used in the Selenium automation framework for maintaining and structuring test code. The Page Object Pattern removes code redundancy and reduces the dependencies between the test code and the elements on the web page.

This pattern promotes good coding practices by maintaining the DRY principle.

DRY Principle and UI Element Changes

The DRY principle means “Don’t Repeat Yourself” and is a best programming practice that promotes efficient code. When working on complex web pages, changes to the UI elements can cause the code to break apart when the project becomes more extensive.

The idea is to have a single source of truth for the elements through a mechanism called “Page Object Pattern.”to Page Object Pattern

The Page Object Pattern is designed to represent each page, its elements, and its mechanics as a class. The interface of the class should define how it interacts with the page and include accessors for each element for easy modification of the page.

The Page Object Pattern abstracts away the UI interactions in isolated Page Object classes and provides a useful representation of the web page.

Creating Page Object Classes

Creating Page Object classes starts by identifying and defining the elements of each page in separate classes. For example, the login page will have an email field, password field, and a login button.

These classes should identify the specific element, methods to interact with them, and the field’s importance. The methods can include sending keys, clicks, or retrieving text values.

Similarly, other pages like the Home page can have the Posts feature, and page elements can be represented in a single class.


Automating a browser is an efficient way to carry out tedious tasks on Instagram. Selenium is one such tool that can be used in automating a browser and can interact with different browsers.

The Page Object Pattern is unique to the Selenium framework and promotes efficient coding practices. Understanding the use of classes, interfaces, and accessors can make designing Page Object classes easy.

By implementing this pattern, it is easy to maintain and edit code as the project expands.

Essential Features of InstaPy

InstaPy is a Python-based automation tool for Instagram that allows for various activities to be carried out automatically. The tool has been designed to simplify the automation process, and comes with several essential features.

In this section, we will take a closer look at some of these crucial features and how they work.

Liking Posts

The InstaPy library allows users to like posts based on specific hashtags. This feature is implemented with the “like_by_tags” method, which likes photos that include any of the tags passed as an argument.

The number of likes per tag can be specified by setting the “likes” variable. This feature can be useful in increasing engagement with other posts on Instagram.

Avoiding Inappropriate Posts

The tool also comes with a built-in filter for inappropriate posts. InstaPy offers a “set_dont_like()” method that users can use to add words or phrases that the bot should avoid when liking or commenting on a post.

This feature can be useful in ensuring that the bot does not engage in inappropriate behavior on Instagram by avoiding posts containing words or themes that are inappropriate.

Following Users

InstaPy’s “set_do_follow()” method allows the automation tool to follow users. The user can specify which accounts to follow or decide to auto-follow other users in the same niche.

This feature can be useful in growing a user’s followings and engaging with the target audience.

Leaving Comments

The InstaPy library also offers a feature that enables users to leave comments on posts. The “set_do_comment()” method initiates the comment function, and users can use the “set_comments()” method to specify the content of the comment.

The tool allows users to comment on posts based on different criteria such as location, hashtags, and usernames.

Ending a Session

When using the InstaPy automation tool, it is essential to end the session correctly to avoid unexpected behavior. The “end()” method is used to close the browser and save logs and reports.

This method stops any current operation the bot is performing, closes the browser, and generates a report indicating what was performed, including the number of likes, comments, follows, and unfollows.

Quota Supervisor

The Instagram platform has a set of rules that restrict the number of interactions performed within a specific time frame. For instance, Instagram caps the number of likes and comments one can leave in an hour or a day to prevent spamming.

InstaPy addresses this issue by incorporating the “

Quota Supervisor” feature that enables users to set quotas, hourly and daily limits, to comply with Instagram’s rules.

Setting Quotas


Quota Supervisor allows users to set hourly and daily limits on likes, follows, and comments. This setting ensures compliance with Instagram’s terms of service and prevents actions that exceed the acceptable interaction limit on the platform.

The feature allows users to specify specific actions, and once the allotted quota is reached, the bot will pause and wait out the quota period before resuming.


InstaPy is an automation tool designed to simplify the process of interacting with Instagram. The essential features such as

Liking Posts,

Avoiding Inappropriate Posts,

Following Users, and

Leaving Comments make using the tool even more effective in growing and engaging a user’s audience.


Quota Supervisor feature ensures Instagram rules compliance and helps avoid penalties and restrictions by setting hourly and daily limits. By utilizing the InstaPy library’s abilities with these features, Instagram tasks can be automated and performed more efficiently.

Headless Browser

A headless browser is a browser without a graphical user interface (GUI). Instead, its features are accessed through a command line interface or application programming interface (API).

A headless browser interacts with web pages, and its functionality can be used programmatically. InstaPy’s headless browser feature can be beneficial in terms of performance by reducing the load on a user’s CPU.

Running a

Headless Browser

Running a headless browser requires first initializing the browser instance. This can be achieved using Selenium WebDriver.

To run a headless browser, the specific browser options should be specified in the program. For instance, setting the “headless” option to True will enable the headless browsing mode.

Some other options like the window size, accept language, and user-agent can be set using capabilities.

Analyzing Posts with AI

InstaPy also incorporates Artificial Intelligence (AI) to analyze and identify objects, themes, and texts associated with a user’s posts. ClarifAI is a pre-built AI model used to analyze images on the platform.

Integration of ClarifAI into the InstaPy library allows for

NSFW image detection, enabling the bot to avoid engaging with inappropriate content on Instagram.

Integrating ClarifAI with InstaPy

To use ClarifAI with InstaPy, the user needs to obtain a ClarifAI API authorization key. Once the key is obtained, the user can use it to authenticate data requests between the two platforms.

The ClarifAI API can then be used to analyze images based on specific tags, objects, themes, or texts. InstaPy’s integration with ClarifAI enables users to avoid inappropriate content by identifying

NSFW images and excluding them from the bot’s actions.

This feature can be vital in maintaining a user’s online presence and following ethics policies on the platform.

Image Recognition

InstaPy’s integration with ClarifAI allows users to carry out image recognition, which is useful in identifying specific visual content. This feature can enable the bot to perform activities only on specific posts that meet certain criteria.

For example, one can specify that the bot should only like posts with images of pets or comment on posts that show food. The feature enhances more targeted engagement with the audience on Instagram.

Image Analysis

Image analysis is the process of examining or analyzing images to extract and identify specific features and patterns. InstaPy’s integration with ClarifAI makes it possible to analyze images on the platform in real-time.

These images are classified based on their tags and can be used to determine the images’ theme and content. Analyzing images can also be used to identify content which may be irrelevant or inappropriate, thus filtering out such content.


Not Safe for Work (

NSFW) images on the platform can be detected by the AI model. InstaPy can check the image content for inappropriate themes before performing any actions, thereby ensuring the bot follows ethical guidelines while engaging on Instagram.


NSFW check can be performed on individual images or images included in larger datasets.


In conclusion, the InstaPy automation tool offers a range of features that can be used to interact with Instagram efficiently. The headless browser feature helps reduce CPU usage and allows for better performance, while AI integration with ClarifAI enables image recognition and analysis.

Integrating the

NSFW detection algorithm reduces the possibility of engaging with trying to avoid inappropriate content on the Instagram platform. When using these features, it is important to adhere to Instagram’s terms of service and ethical guidelines to avoid penalties or account suspension.

In conclusion, the article covers the essential features of InstaPy, including

Liking Posts,

Avoiding Inappropriate Posts

Popular Posts