DEV Community

Japneet Singh Chawla
Japneet Singh Chawla

Posted on

Instagram Bot using Python

Growing on Instagram is a difficult and time-consuming job if you are already not a famous personality.
I started my food blogging page @.interwined_dodos. (do check it out and follow me there) and faced these challenges in growing. Some of the very common challenges I have faced were:

  • Interacting with all community throughout the day is a difficult job
  • Instagram blocks the like, comment, follow and unfollow actions if you are over interactive in a short span of time *Following and engaging with new people of same or different community(again a time-consuming job)

Being a lazy yet ambitious person, I wanted to grow on Instagram but didn't want to spend much time on it.
Here is when my developer mind kicked in and I thought of automating this stuff and my journey of this automation lead me to blogs related to selenium and then to my destination Instapy.

InstaPy is a tool developed to automate Instagram tasks using Selenium and the brain. It has a large variety of actions that can be performed on Instagram by just a few lines of code. Some popular actions are:

  • Smart liking
  • Smart Commenting
  • Following
  • Unfollowing

I said smart liking and commenting because there are lots of configuration options that are available in the InstaPy to give it a human-like touch. Also, it is carefully crafted so that you don't get blocked by Instagram (but it can happen and you can get blocked for months too if you are not careful).

Frankly speaking, I even got blocked by Instagram for manually performing the actions and I am using this bot from past 2 days and things are fine as of now.
Let's get started.....

Installation

Running InstaPy needs Firefox browser installed so install the version as per your operating system from this link.

I will prefer version 0.6.8 because there are some issues with comments in the latest version 0.6.9, so do a quick pip install to install the package
pip install instapy==0.6.8

The Code

""" Quickstart script for InstaPy usage """
# imports
from instapy import InstaPy
from instapy import smart_run
# login credentials
insta_username = ''  # <- enter username here
insta_password = ''  # <- enter password here
# get an InstaPy session!
# set headless_browser=True to run InstaPy in the background
session = InstaPy(username=insta_username,
                  password=insta_password,
                  headless_browser=False)
with smart_run(session):
    """ Activity flow """
    # general settings
    #sets the percentage of people you want to follow
    session.set_do_follow(True, percentage=50)
    #sets the percentage of posts you want to comment
    session.set_do_comment(True, percentage=100)
    #list of random comments you want to post
    session.set_comments(["hey @_.interwined_dodos._, have a look", "Great content @_.interwined_dodos._ have a look", ":heart_eyes: :heart_eyes: :heart_eyes: @_.interwined_dodos._"])
    #setting quotas for the daily and hourly action(I said it's smart)
    session.set_quota_supervisor(enabled=True, peak_comments_daily=250, peak_comments_hourly=30, peak_likes_daily=250, peak_likes_hourly=30,sleep_after=['likes', 'follows'])
    #again some set of configuration which figures out whom to follow
    session.set_relationship_bounds(enabled=True,
                                    delimit_by_numbers=True,
                                    max_followers=4590,
                                    min_followers=45,
                                    min_following=77)
    #tags to get posts from and amout is the actions you want 
    session.like_by_tags(['nailsofinstagram',"nails"], amount=300)

That's it...
With these few lines your bot is ready to roll

Run the Bot

python <your_file_name>

I never thought that creating bot will be this easy. I hope this help many lazy people like me :P

Also, have look at my other blogs:

Extracting text from PDF
Using Celery for Heavy Loads
Scraping the Web

Top comments (0)