How to gather information from Instagram with Instaloader python tool


Being a Python coder, I like to experiment with all kinds of open source tools shared on the major platform Github through which coders from the whole world develop projects by collaborating with each other. As an Instagram user, I decided to try some Python applications which can help to interact with it from the command line. 

Instaloader

According to the official documentation, instaloader can be used to download information from different Instagram accounts. The following is the type of information one can gather through the usage of the instaloader python application.
  • posts based on hashtags
  • geotags
  • captions for each post
  • stories
  • photos
  • feeds
  • saved media
  • videos

How to setup Instaloader on your machine 

Being dependent on other python libraries, it is required that we create a virtual environment with the main purpose of installing the whole dependencies of the project. First of all make sure to clone the project with the help of the command shown below.
git clone https://github.com/instaloader/instaloader.git
Once you have managed to clone the project on your local machine, navigate to the project with the help of the command shown below.
cd instaloader
Then create a fresh virtual environment with the help of the command shown below.
virtualenv -p /usr/local/bin/python3.7 env # provide the absolute python path
source env/bin/activate
Generate the dependencies of the project by making use of the command shown below.
pip3 install pipenv
pipenv run pip3 freeze > requirements.txt
Install the dependencies.
pip3 install -r requirements.txt

How to download your first profile from Instagram with Instaloader

Now that we managed to install the entire dependencies of the project, we can easily download a public profile from Instagram with the help of the command shown below.
python instaloader.py artofyasharala
By a simple look at the above command, it is easy to spot that we execute the python script named instaloader.py and provide the public username of the account which we're trying to download. Once I managed to execute the command shown above on my console, I got the following output.
Stored ID 25946397639 for profile artofyasharala.
Warning: Use --login to download HD version of profile pictures.
artofyasharala/2019-12-14_17-25-44_UTC_profile_pic.jpg
Retrieving posts from profile artofyasharala.
[1/14] artofyasharala/2020-01-30_04-07-11_UTC_1.jpg artofyasharala/2020-01-30_04-07-11_UTC_2.jpg artofyasharala/2020-01-30_04-07-11_UTC_3.jpg [🦁Yahawashi WIP 2  Revelation …] json
[2/ 14] artofyasharala/2020-01-29_03-47-20_UTC.jpg [🦁Yahawashi WIP 1  Revelation …] json
[3/14]artofyasharala/2020-01-10_05-03-18_UTC_1.jpg artofyasharala/2020-01-10_05-03-18_UTC_2.jpg [🦁Righteous Bonding Time WIP 2…] json 
The Instaloader application informed us about the number of pictures it is downloading, one by one. The program automatically creates a directory for saving the data with the exact name provided for the username.

For each post downloaded, instaloader saves details in a JSON file. As for the photo captions, they're being saved inside text files.

For example, the following command displays a downloaded caption on my console.
cat artofyasharala/2019-12-15_03-29-06_UTC.txt

How to download comments for each post

As for the comments on each post, they're not automatically downloaded. The option --comment can help to download the whole comments by post.
python instaloader.py artofyasharala --comment
Once I executed the command shown above, comments got downloaded.
2019-12-14_17-25-44_UTC_profile_pic.jpg 2019-12-19_04-04-37_UTC_comments.json 2020-01-02_01-33-41_UTC_comments.json
2019-12-15_03-29-06_UTC.json 2019-12-19_23-49-58_UTC.jpg 2020-01-03_21-26-45_UTC.json.xz
2019-12-15_03-29-06_UTC.json.xz 2019-12-19_23-49-58_UTC.json.xz 2020-01-03_21-26-45_UTC.txt
2019-12-15_03-29-06_UTC.txt 2019-12-19_23-49-58_UTC.txt 2020-01-03_21-26-45_UTC_1.jpg
2019-12-15_03-29-06_UTC_1.jpg 2019-12-19_23-49-58_UTC_comments.json 2020-01-03_21-26-45_UTC_1.mp4
2019-12-15_03-29-06_UTC_2.jpg 2019-12-20_22-59-22_UTC.json.xz 2020-01-03_21-26-45_UTC_2.jpg
2019-12-15_03-29-06_UTC_comments.json
As you can see from the above output, comments are being saved as JSON files.

How to avoid captions from being downloaded as text files

Being downloaded automatically, it is possible to turn off the download of captions as text files with the help of the --no-captions option.
python instaloader.py artofyasharala --no-captions

How to avoid storage of post's metadata as JSON

For you to stop downloading and saving each post's metadata as JSON files, the --no-metadata-json  is required.
python instaloader.py artofyasharala --no-metadata-json
Another useful option is the one which helps to avoid the xz automatic compression of JSON files. It is being illustrated below.
python instaloader.py artofyasharala --no-compress-json

How to download geotags for each image, if available

It is also possible to extract the geotag for each image if it is available. The option which can help us to do that is the --geotags. The usage of such option requires a login.

Let's try the following command.
python instaloader.py artofyasharala --geotags
When I executed the above command on my local console, instaloader informed me that I need to login first. To login make use of the following command.
python instaloader.py artofyasharala --login wisdomovermoney13 -p password_here --geotags
When logging in, the instaloader creates a session file which it uses for the next requests that require login. So the next time you want to make a request, just specify the profile you want to login into and instaloader will automatically load the session file.
python instaloader.py artofyasharala  --login  wisdomovermoney13 --geotags
Once I executed the above command on my console, I got the following output.
Loaded session from /var/folders/vt/nqlv_f8j27j4c136rkjgjd580000gn/t/.instaloader-oltjano/session-wisdomovermoney13.
Logged in as wisdomovermoney13.

How to download stories from the Instagram profile

Another option which requires to be logged in, is the --stories. The following command illustrates such option in action.
python instaloader.py artofyasharala  --login  wisdomovermoney13 --stories
After the execution of the above command on my console, I noticed a change in the output.
Downloading stories
Retrieving stories from profile artofyasharala.
[  1/  2] artofyasharala/2020-01-31_02-21-05_UTC.jpg exists json
[  2/  2] artofyasharala/2020-01-31_02-20-38_UTC.jpg exists json 
It's truly clear that instaloader downloaded the stories from the specified profile, which in my case is artofyasharala.

A recommended option for each download

The option --fast-update helps to save time while downloading Instagram accounts. Having an already downloaded archive, this option is recommended for updates.
python instaloader.py artofyasharala  --fast-update

How to receive only basic information for an Instagram profile

The following command helps to download basic information for an Instagram account as a JSON file.
python instaloader.py artofyasharala  --no-posts --no-profile-pic --no-compress-json
Once I managed to execute the above command on my console I got a JSON file with the output shown below.
{
    "instaloader": {
        "node_type": "Profile",
        "version": "4.2.10"
    },
    "node": {
        "biography": "\ud83e\udd81Israelite #Artist! #hebrewisraelites \ud83e\udd81\nDM me for artworks, posters, album covers, painting, etc. Commissions Open!\nDonations $armelle144\n\ud83d\udc51Levite\ud83d\udc51",
        "blocked_by_viewer": false,
        "business_category_name": null,
        "connected_fb_page": null,
        "country_block": false,
        "edge_felix_video_timeline": {
            "count": 0,
            "edges": [],
            "page_info": {
                "end_cursor": null,
                "has_next_page": false
            }
        },
        "edge_follow": {
            "count": 385
        },
        "edge_followed_by": {
            "count": 280
        },
        "edge_mutual_followed_by": {
            "count": 0,
            "edges": []
        },
        "external_url": "http://cash.app/$armelle144",
        "external_url_linkshimmed": "https://l.instagram.com/?u=http%3A%2F%2Fcash.app%2F%24armelle144&e=ATOdY2BMJbSvLdBb45becPY8jw_E7N4hPFOYwEI1m0yHocPJwc2LMCMF1WhKzeC0Mk5ajmCL",
        "followed_by_viewer": false,
        "follows_viewer": false,
        "full_name": "Art of Yasharala",
        "has_blocked_viewer": false,
        "has_channel": false,
        "has_requested_viewer": false,
        "highlight_reel_count": 2,
        "id": "25946397639",
        "is_business_account": false,
        "is_joined_recently": false,
        "is_private": false,
        "is_verified": false,
        "profile_pic_url": "https://instagram.ftia5-1.fna.fbcdn.net/v/t51.2885-19/s150x150/75252746_2560876454178722_8510616106640080896_n.jpg?_nc_ht=instagram.ftia5-1.fna.fbcdn.net&_nc_ohc=1O3duYgbbRIAX9tphDa&oh=ea2c855c87beeca6004d5e0298afe154&oe=5EBF8413",
        "profile_pic_url_hd": "https://instagram.ftia5-1.fna.fbcdn.net/v/t51.2885-19/s320x320/75252746_2560876454178722_8510616106640080896_n.jpg?_nc_ht=instagram.ftia5-1.fna.fbcdn.net&_nc_ohc=1O3duYgbbRIAX9tphDa&oh=9261792a013532905ac31135d0349d8e&oe=5ED5E16B",
        "requested_by_viewer": false,
        "username": "artofyasharala"
    }
}
The output is pretty informative. The basic output provides us with the username, the number of followers the account has, the number of people he or she follows, and also the biography.

How to receive only the tagged posts for the Instagram profile

The command shown below helps to download each one of the posts where the profile has been tagged.
python instaloader.py artofyasharala  --no-posts --no-profile-pic --no-compress-json --tagged

How to download only the comments for each post

The instaloader package can also be used to download comments for each post as a JSON file. The command shown below can be used to accomplish the task.
python instaloader.py artofyasharala   --no-profile-pic --no-compress-json --comments

How to download all followers of a profile

With the help of the instaloader we can easily download followers of a specific profile by making use of the command shown below.
python instaloader.py @artofyasharala --login wisdomoverymoney13 --no-profile-pic --no-compress-json 
After the execution of the above command I received an output similar to the one shown below.
Loaded session from /var/folders/vt/nqlv_f8j27j4c136rkjgjd580000gn/t/.instaloader-oltjano/session-wisdomovermoney13.
Logged in as wisdomovermoney13.
Retrieving followees of artofyasharala...
As you can see from the output shown above, the program is retrieving followers of the profile we have specified.

How to retrieve posts based on a hashtag

Retrieving data from Instagram based on hashtags, is my favorite option of the Instaloader. To accomplish such task, make use of the command shown below.
python instaloader.py  "#hacker"  --no-compress-json
Once I managed to execute the above command on my console, I got an output similar to the one shown below.
Retrieving pictures with hashtag hacker...
[  1] #hacker #hacker/2020-01-31_19-36-03_UTC.jpg [My first photography hope you…] json
[  2] #hacker #hacker/2020-01-31_19-32-57_UTC.jpg [¿? . . . #shitposting #churra…] json
[  3] #hacker #hacker/2020-01-31_19-32-34_UTC.jpg [LET'S GET BOMBED! i.e., Shmoo…] json
[  4] #hacker #hacker/2020-01-31_19-30-35_UTC.jpg [Sextou com S de SOCORRO agost…] json
[  5] #hacker #hacker/2020-01-31_19-30-28_UTC_1.jpg #hacker/2020-01-31_19-30-28_UTC_2.jpg [#pubgmobile #pubg #bridgebloc…] json
[  6] #hacker #hacker/2020-01-31_19-28-50_UTC.jpg [Corona virus is no more guys …] json
[  7] #hacker #hacker/2020-01-31_19-27-16_UTC.jpg [Hacking radio frequencys #hac…] json
[  8] #hacker #hacker/2020-01-31_19-26-17_UTC.jpg #hacker/2020-01-31_19-26-17_UTC.mp4 json
Interrupted by user.
It is also possible to specify multiple hashtags.
python instaloader.py  "#hacker" "#python"  --no-compress-json 

How to disable the download of videos

If you don't like to extract videos while downloading an Instagram profile, just make use of the --no-videos option like shown below.
python instaloader.py artofyasharala  --no-compress-json --no-videos

How to put instaloader in quiet mode

Although the messages of the program instaloader are useful, especially when it comes to debugging; sometimes you may need to disable the user interaction. The option --quiet can help to disable the verbose mode.
python instaloader.py artofyasharala  --no-compress-json --quiet

Advanced options 

Some of the advanced options include --user-agent, --commit-mode, --request-timeout and also the --max-connection-attempt.
For example you can specify the custom user agent with the help of the command shown below.
python instaloader.py artofyasharala --user-agent "apple"
As for the --commit-mode, it becomes helpful when there are unexpected interruptions during the download of images from the Instagram profile.
python instaloader.py artofyasharala --commit-mode
The option --request-timeout can be used to specify the number of seconds to wait before timing out a connection request.
python instaloader.py artofyasharala --request-timeout 0.5
The option --max-connection-attempt can be used to specify the maximum number of connections until a request is aborted.
python instaloader.py artofyasharala --max-connection-attempt 1
Copyright hackermilk.blogspot.com  

Been passionate about computer technologies I have started this blog to document my journey as an amatuer hacker. Having some past experience as a technical writer for open source websites my aim is to produce high quality content which is helpful to anyone who wants to push their hacker skills to the next level.

Latest
Previous
Click here for Comments

0 Comments: