Reputation: 401
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:
Does anyone have a solution for this?
Thank you!
Upvotes: 30
Views: 36838
Reputation: 326
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;
?>
Upvotes: 0
Reputation: 586
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
Upvotes: 25
Reputation: 156
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.
Upvotes: 4
Reputation: 142
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
Upvotes: 6