Nerd For Tech
Published in

Nerd For Tech

Scrape Instagram using python

This post talks about how we can connect to Instagram using python and extract list of followers, people whom you follow and the list of people you should unfollow (people whom you follow but they don’t 😛)

We can easily do the above using a built in python package called instaloader. In case you directly want to jump to code and see it in action here it is -


  1. Python3
  2. Instaloader
  3. Numpy


Create a file which will handle the below 4 steps

  1. Create a session

We get an instance of Instaloader in below codeblock and login using the username and password provided by the user. Once that is done, profile instance is created in order to fetch the profile’s metadata.

def create_session(self):        L = instaloader.Instaloader()
L.login(self.username, self.password) # Login or load session
self.profile = instaloader.Profile.from_username(L.context, self.username) # Obtain profile metadata

2. Get list of followers

def scrape_followers(self):        for follower in self.profile.get_followers():

3. Get list of following

def scrape_following(self):        for followee in self.profile.get_followees():

4. Get unfollow list

This would generate a unfollowers_<USERNAME>.txt file in your present directory containing the list of people whom you follow but they don't.

def generate_unfollowers_list(self):        unfollow_list = np.setdiff1d(self.following_list, self.followers_list) # unfollow people who are only in following list and not in followers list
print("People to unfollow: ", unfollow_list)
filename = "unfollowers_" + self.username + ".txt"
file = open(filename, "w")
for person in unfollow_list:
file.write(person + "\n")

The code can then be executed from a runner script which would invoke create_session() using the username and password of the user. The design has been kept in such a way so as to make sure user's username and password is only needed while creating session post which we can directly invoke APIs scrape_followers() etc as per the requirement.

Instaloader is a very efficient package. We can do much more using it. Please check the documentation here for more details:

You can find the entire running code here with a README to provide steps to run.

This is it for this article. Happy learning!! ❤

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit

Recommended from Medium

Designers Cheatsheet to get yourself interested in coding

5 Python Search Scripts

OpenBOM Professional subscription for Onshape

Basics of API -Cyber Sapiens Internship Task-24

Configuring WordPress With MySQL Database over AWS Using AWS RDS

Build with me-building my Core App

See Yearn Finance at ETHDenver 2022

How do AWS developers manage Web apps?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Apoorva Dave

Apoorva Dave

Codes a little 💻, eats a lot 🍕

More from Medium

Web-scrapping in Python for job hunting (part 1)

A magnifying glass on top of a book in German

Python Beginner tutorials — Day 01

How I Made an Automatic B’day Wisher Using Python

Building a Python Chatbot — Part I