← Back to list

Scraping Like Data In Instagram Post Using Python

This article will create a program for scraping all user data who likes a post on Instagram using python. We will take user data username…

Sencho Parameswara · 2022-03-10 08:22 · 16 claps · 4.9 min read
#web-scraping #python #python-scraping #instagram-scraping #json-scraping
Open on Medium ↗

Scraping Like Data In Instagram Post Using Python

Photo by Prateek Katyal on Unsplash

Photo by Prateek Katyal on Unsplash

This article will create a program for scraping all user data who likes a post on Instagram using python. We will take user data username, full name, profile picture link, and account status, whether private or not. The scraping results will be extracted into a CSV file and added in real-time, and each CSV file holds 250 pieces of data. The first time this program is run, it will ask for a shortcode for a post explained later. After that, the program will start scraping all user data who likes the post. In this program, we only need requests because the data we will take is JSON. In addition, we also need JSON, os, and CSV packages to extract data in CSV form and package time to avoid scraping failures due to too much data access.

For the first step, we first open the Instagram website, enter the post to take the like data, open the like section, and inspect. In inspect, you select a network, select Fetch/XHR, and select it with the name query_hash like the example image below.

If you have found data like the one above, that data is collected for users who like the post. Then we will access the data using the URL that I have blocked in the picture above. Still, we only take the URL until the query because we will fill in the advanced parameters ourselves. We store the URL in a variable named url.

Next, we create some variables that serve as additional parameters for package requests. First, we create a variable named short_code, which serves to accommodate the shortcode we want. Short_code itself is a variable with a unique code to access specific posts. Then we create a variable end_crusor with an empty value, this end_crusor functions as pagination in Instagram. The purpose of the end_crusor variable is empty because we want to access all likes data in one post.

Next, we create a count variable with a value of zero, this variable functions as a number for each data. Later at the end of the code, we call this variable to find out the total data obtained. Next, we create two variables to make one extraction file hold as many as 250 pieces of data. First, we create a start_file variable with one, which serves as the initial filename. Then we create a variable named per_file with a value of 250, which serves as a marker for the amount of data in one file.

Next, we create a folder to accommodate our extracted files using try-except. Finally, we create an empty CSV file with the name and file to be flexible according to the existing data. Like our initial code

[embed]

Next, we start scraping data. We make code scraping inside while looping because we want to loop every data in each end_crusor which is the pagination of Instagram likes. In the loop, while true, we define a variable named variable. To understand the contents of the variable itself, we can look at the results of the previous inspect and select the payload section as shown below.

As I have blocked in the variable section, it is the content of the variable we created. First, in the shortcode section, you fill in the short_code variable. For the first, we fill it with a value of 50. This is the amount of user data that appears on each page, for the maximum number of pages that can be displayed per page is 50. We use the last one after, and we fill it with the end_crusor value because each page has a different after value, and we will loop it until the last page.

Next, we create a params variable that contains the overall parameters as shown in the picture above, including the variables we input with variable values, but we create them in JSON format. And finally, we create a head variable by filling in the session id in the cookie section. We create a req variable that contains package requests with the get function, input the url variable, and the params and head parameters that we created earlier. And don’t forget to add JSON at the end of the code because our target data is JSON. Like this is our temporary code.

[embed]

Next, we start scraping data. If we look at our target JSON structure like this

If you see, everyone’s data is at the key edges. We scrap the data. The method is the same as taking the key in a dictionary to scrap the JSON data. Still, because Instagram has limitations on data collection, we use try-except. If it fails to retrieve data, there will be a waiting time of 30 seconds. After that, try re-scraping on the same page.

Next, we start retrieving data using a for loop. Still, at the beginning of the loop, we create a branch, where if the count reaches 250, we create a new file to hold our data. The start_file variable is added by naming because our program holds 250 data in each file. After that, we start taking all the data we need, and for sure, we have to add 1 to the value of the count variable for each iteration so that the count is correct. We call the data we have got. Finally, we add code that adds live data without waiting for all data to be taken and entered into the file. Like this code we have made.

[embed]

For the last stage, we will look for a new page by taking the latest end_crusor data, and we will also look for the has_next_page data, which serves as a marker whether there is an advanced page or not. And finally, we define where the has_next_page variable is false, then the while loop is stopped. And don’t forget to give a two-second pause for every new scraping page to avoid blocking access by Instagram. Note that this code is created under the while loop. Like this code

[embed]

Our program is finished, we can run our program according to what we expected initially, namely scraping all user data who likes a post on Instagram using python. We will take user data username, full name, profile picture link, and account status, whether private or not. The scraping results will be extracted into a CSV file and added in real-time, and each CSV file holds 250 pieces of data. I’ve also put this code in the Github repository here. You can clone and then change the code.


메타데이터
post_id
6072bb2ef6b8
slug
scraping-like-data-in-instagram-post-using-python-6072bb2ef6b8
url
https://medium.com/@senchoo/scraping-like-data-in-instagram-post-using-python-6072bb2ef6b8
canonical_url
https://medium.com/@senchoo/scraping-like-data-in-instagram-post-using-python-6072bb2ef6b8
author_url
https://medium.com/@senchoo
status
ok
fetched_at
2026-06-20 20:29:01