We earn commission when you buy through affiliate links.

This does not influence our reviews or recommendations.Learn more.

This article is a guide on how to programmatically downloadInstagramdata from a profile using Python in two methods.

Screenshot-from-2023-04-26-14-15-20

The first method is downloading media using Instaloader.

The second is writing a simple Python script to get JSON data about the profile.

Using Instaloader

Instaloaderis a Python package for downloading Instagram media.

Screenshot-from-2023-04-26-14-17-41

It is incredibly easy to use and makes extracting and downloading data quick and easy.

To use it from the command line, you use theinstaloadercommand.

Like so:

But for this command to work, it’s crucial that you get in first.

Screenshot-from-2023-04-26-14-19-14

An alternative to using the Instaloader command line interface is to use it as a Python package.

The package is well documentedhere.

With Instaloader, you could download different media files.

Screenshot-from-2023-04-27-08-40-59

With the next method, you will write a Python Script to extract a user profiles data.

This method relies on using a relatively unknown Instagram JSON API for extracting data from public profiles.

For example, my username is 0xanesu.

Screenshot-from-2023-04-26-14-26-39

To begin, install therequestsmodule usingpip.

In addition, also import the loads function fromjson.

This will be used to parse the JSON response.

once you nail imported the data, create a variable that stores the URL to your Instagram profile.

As mentioned before, to get to extract Instagram data from a profile, you better add the__a=1and__d=1query parameters.

To define those, we create a dictionary object with the parameters.

To authorize requests we make, Instagram requires a Session ID.

Later on, I will show you how to get your Session ID.

For now, just put a placeholder value that you will substitute later.

Next, define a function that will run when the request is successful.

After this, I am only extracting the full name and biography of the profile.

Next, define the function that will run if there is an error.

Then lastly, we check the status code of the error.

If the status is 200, we call theon_successfunction.

Else we just call theon_errorfunction.

At this point, we are done writing the code.

Whats left is to get thesessionid.

To get the session id, open your Google Chrome and open Instagram on Web.

Ensure you are logged in, then open Dev Tools usingCtrl + Shift + IorCmd +Shift + I.

With Dev Tools open, initiate the app tab.

Then nudge the Cookies submenu to view Cookies used by Instagram.

Once you copy the session id, paste it into the script and execute the script.

And just like that, we are able to dynamically download profile data.

There is so much more data that is returned from the JSON API.

Final Words

In this article, we went through how to download posts and media using Instaloader.