Scrape Instagram Followers with Python

David Wicker
Python InstaClient
4 min readOct 18, 2020

--

In this article I’ll explain how you can easily scrape an Instagram account’s followers with Python, taking advantage of the InstaClient package.

The InstaClient package is based on Selenium and acts as if the bot were a normal Instagram User.

Find Help

First off, you can check InstaClient’s source code at https://github.com/wickerdevs/instaclient/ and you can find help in the following Telegram group: https://t.me/instaclient

Getting Started

I’m going to assume you already have Python 3.7 or higher installed on your machine. If that’s not the case, you can check out this simple tutorial: https://realpython.com/installing-python/

The first thing I suggest you do is to create a python enviroment were to install the required packages for your code. You can do this simply by entering the following commands in your terminal, once you have navigated to the desired folder (use cd in your terminal to navigate between folders):

python -m venv .env

This command creates an .env folder with all the necessary files. You won’t need to worry about that folder for now.

Then, you can activate the enviroment by typing:

.env/Scripts/env

At this point, we can go ahead and install the instaclient library using pip:

pip install -U instaclient

Instagram Scraper

To create your own, simple, follower scraper you can create a new file in your project folder and name it bot.py (or something else) and insert the following code in it. (The code will be explained step by step below)

Analizing the code

Create a client instance

Let’s start with the first thing: creating an InstaClient instance. You can achieve that by writing

client = InstaClient()

Login

We can then use that client to log into our instagram’s account:

result = client.login(username='username', password='password')

This line of code will raise the following exception:

  • InvalidUserError → if the username you insterted does not exist.
  • InvalidPasswordError → if the password is invalid.
  • VerificationCodeRequired → if your account has 2FA turned on and Instagram needs a security code to log in.
  • SuspiciousLoginAttemptError → this will be raised if instagram detects suspicious activity on your account and asks for manual verification.

If you catch a SecurityCodeRequired exception, you will need to provide the security code to log in. You can do that by calling:

result = client.input_security_code(code=<YOUR CODE HERE>)

In case the provided security code is incorrect or has expired, the InvalidSecurityCode exception will be thrown.

If you catch that exception, you can easily try again by calling the same line of code again but with a different code:

result = client.input_security_code(code=<ANOTHER CODE ATTEMPT>)

At this point, you can check whether login was successful by checking the value of the result variable: the login() and input_security_code() methods will return True if login was successful.

Alternatively, you can check whether you have successfully logged in by calling client.logged_in (This variable is True if login was successful)

Scraping Followers

Once you are logged in, you can scrape any public user’s followers with the following line of code:

followers = client.get_followers(user='username', count=150, callback_frequency=15, callback=None)

Let’s now analyze what the count, callback_frequency and callback fields mean:

  • The count field tells the client how many followers to retrieve. Since getting a user’s follower is an expensive task (can take along time), it’s best to specify how many followers you actually need to retrieve. Note that the higher the count, the longer the task will take to execute
  • The callback_frequency field tells the client how often to send a feedback of the scraping progress: since the process might take long, it is helpful to check whether everything is working alright. By default callback_frequency is set to 10, hence the client will execute a callback every 10 scraped followers.
  • The callback field tells the client what callback to execute. By default, the client will simply print ‘Got <N. OF USERS> users so far…’. The callback can be customized (it has to be a callable method), for example you set it to send a Telegram message to your account.

Hosting your scraper

The simplest way to run the scraper is to run it on your local machine, but you can also upload it to a host (such as Heroku).

If you decide to upload your scraper to a server, you should add it to a worker and you should follow the steps detailed in this article:

--

--

David Wicker
Python InstaClient

Developer for Fridays For Future. Freelancer on Fiverr. Experienced in Python, Java, C#.