DEV Community

Cover image for Scrape Instagram using python
Apoorva Dave
Apoorva Dave

Posted on

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

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
Enter fullscreen mode Exit fullscreen mode
Get list of followers
  def scrape_followers(self):

        for follower in self.profile.get_followers():
Enter fullscreen mode Exit fullscreen mode
Get list of following
def scrape_following(self):

        for followee in self.profile.get_followees():
Enter fullscreen mode Exit fullscreen mode
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")
Enter fullscreen mode Exit fullscreen mode

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!! <3

Top comments (3)

francisjacob profile image
Francis jacob

Apoorva is the developer we need but do not deserve. Time to unfollow some instagram "friends".

favanso profile image
Fernando Branco

Really interesting

kareemrasheed89 profile image
Rasheed Kareem