How to get instagram follower count from instagram public account after 2020 instagram api change?
Asked Answered
C

4

30

I am trying to retrieve the follower count only just using a user's instagram handle (aka @myusername). I read in several answers that you can access the "https://www.instagram.com/{username}/?__a=1" to retrieve the block of json that has all the details. However, when I try to do that now in 2020 after the api change, the url simply redirects me to the login page.

I also looked at instagram basic display/graph api but I can't seem to find a way to get other users' follower counts.

From the official ig basic display api documentation: https://developers.facebook.com/docs/instagram-basic-display-api/reference/user
This api only allows you to get the account_type, id, ig_id (about to be deprecated), media count and username. (no sign of follower count)

From the official ig graph api documentation: https://developers.facebook.com/docs/instagram-api
"The API cannot access Intagram consumer accounts (i.e., non-Business or non-Creator Instagram accounts). If you are building an app for consumer users, use the Instagram Basic Display API instead."

Since my program is suppose to retrieve the follower count from accounts that are not necessarily professional, I can't seem to figure out what to do...

In conclusion:

  • I have tried web scraping -> get redirected to login page
  • Basic display api -> can't get the follower count
  • Graph api -> can't access consumer accounts

Does anyone have a solution for this?

Thank you!

Casualty answered 2/9, 2020 at 16:44 Comment(3)
@Hyunk Have you got any solution?Malka
Hey @hyun-seok-cho have you managed to find a solution for this?Wardlaw
did anyone ever found a solution?Girhiny
L
23

Had the same problem. I ended up inspecting instagram network and found this:

https://i.instagram.com/api/v1/users/web_profile_info/?username=<USERNAME>.

Make sure you put a user-agent header in the request with this value:

Instagram 76.0.0.15.395 Android (24/7.0; 640dpi; 1440x2560; samsung; SM-G930F; herolte; samsungexynos8890; en_US; 138226743)

you can get the follower count on data.user.edge_followed_by.count

Latour answered 16/8, 2022 at 15:5 Comment(3)
I hope this method stays and is not blocked by Insta/FbOlimpia
Above method worked for me after adding following 2 headers as well: Origin: "https://www.instagram.com", Referer: "https://www.instagram.com/". Otherwise, it was throwing SecFetch Policy violation.Prophylactic
use this to bypass the login page cookies = { "sessionid": "value", "csrftoken": "value" } response = requests.get(url, headers=headers, cookies=cookies)Marybellemarybeth
B
5

You can use Instaloader Python module. Here is a quic example:

import instaloader
L = instaloader.Instaloader()
user = "IG_USERNAME"
password = "IG_PASSWORD"
L.login(user, password)
profile = instaloader.Profile.from_username(L.context, user)

print(profile.followees) #number of following
print(profile.followers) #number of followers
print(profile.full_name) #full name
print(profile.biography) #bio
print(profile.profile_pic_url)  #profile picture url 
print(profile.get_posts()) #list of posts
print(profile.get_followers()) #list of followers
print(profile.get_followees()) #list of followees
Biff answered 13/10, 2020 at 23:28 Comment(1)
But it is blocked when iterating on a list of users.Miquelmiquela
A
4

Instagram blocked your IP ... you need to use undetected proxies to scrape that from Instagram, usually, Residential IPs passed.

send HTTP request to IG API end > if got JSON response it's good and not detected, else it's detected proxy and not good for Instagram.

code for that (in c# but could follow the same logic in python) :

var httpClientHandler = new HttpClientHandler()
            {
                Proxy = new WebProxy("127.0.0.1:8080", false),
                UseProxy = true
            };

            var client = new HttpClient(handler: httpClientHandler, disposeHandler: true);
            var response = await client.GetAsync("https://www.instagram.com/instagram/?__a=1");

        
            if ( response.Content.Headers.ContentType.MediaType == "application/json")
            {
                // Not detected proxy and good for Instagram

            }
            else
            {
               // Detected proxy and not good for Instagram
            }

If you ask why Instagram is blocking some IPs? simply Instagram has a huge database that contains past IPs who sent more than the allowed limit requests ... they are doing that to prevent scraping.

Atrip answered 6/11, 2020 at 1:34 Comment(2)
Thanks, I will try and get back to you with my results!Casualty
use fiddler or similar tool to catch the HTTP response with status code, see if they still blocking your IP...Atrip
M
0

PHP code using PHP curl:

<?php
$username = 'USERNAME'; // Replace 'USERNAME' with the Instagram username you want to query

// Set up request URL
$request_url = 'https://i.instagram.com/api/v1/users/web_profile_info/?username=' . $username;

// Set up headers
$headers = array(
    'User-Agent: Instagram 76.0.0.15.395 Android (24/7.0; 640dpi; 1440x2560; samsung; SM-G930F; herolte; samsungexynos8890; en_US; 138226743)',
    'Origin: https://www.instagram.com',
    'Referer: https://www.instagram.com/'
);

// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the request
$response = curl_exec($ch);

// Check for errors
if(curl_errno($ch)){
    echo 'Curl error: ' . curl_error($ch);
}

// Close cURL session
curl_close($ch);

// Decode JSON response
$data = json_decode($response, true);

// Extract the follower count
$follower_count = $data['data']['user']['edge_followed_by']['count'];

// Output the follower count
echo "Follower count: " . $follower_count;
?>
Mellow answered 10/3, 2024 at 14:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.