Search code examples
c#facebookweb-scrapinginstagraminstagram-api

Get instagram full profile information without "Instagram Graph Api"


I wanna get some data from the Instagram users.

So I've used Instagram Basic Display Api and the profile data I could receive was these:

  • username
  • media count
  • account type

but I want these data:

  • username
  • name
  • media count
  • Profile Image
  • followers count
  • following count

I don't know how can I have these data without Instagram Graph API(in any way) in c#?

Or is there any way to get these data with the WebClient class or anything like that?

Update for @Eehab answer: I use RestClient and WebClient in this example and both of them give the same result.

Now see WebClient example:

WebClient client = new WebClient();
string page = client.DownloadString("https://www.instagram.com/instagram/?__a=1");
Console.WriteLine(page);
Console.ReadKey();

and see an image of this code here.

now see the result of the code above here

I've also got, that this link is the only access for login users and I've been login into my Instagram account in chrome already, but I think WebClient needs to log in too.

Edit Through @Eehab answer:

In this case for using this Url(https://www.instagram.com/{username}/?__a=1), we can't do it without Instagram logged-in browser profile. So we should log in to Instagram with selenium and use the logged-in cookies to use it for Url requests. So first Install the selenium web driver and then write the following codes(untested):

var driver = new ChromeDriver();

//go to Instagram
driver.Url = "https://www.instagram.com/";

//Log in
var userNameElement = _driver.FindElement(By.Name("username"));
userNameElement.SendKeys("Username");
var passwordElement = _driver.FindElement(By.Name("password"));
passwordElement.SendKeys(Cars[0].auth.pass);
var loginButton = _driver.FindElement(By.Id("login"));
loginButton.Click();

//Get cookies
var cookies = driver.Manage().Cookies.AllCookies.ToList();

//Send request with given cookies :)
var url = "https://www.instagram.com/{username}/?__a=1";
var httpRequest = (HttpWebRequest)WebRequest.Create(url);
foreach(var cookie in cookies){
   httpRequest.Headers["Cookie"] += $"{cookie.Name}={cookie.Value}; ";
}
var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
   var result = streamReader.ReadToEnd();
}
//...

If anyone can improve this question for more uses can edit and I really appreciate it :)


Solution

  • You could do that using the open API , example :

    https://www.instagram.com/instagram/?__a=1

    example code from postman code :

    var client = new RestClient("https://www.instagram.com/instagram/?__a=1");
    client.Timeout = -1;
    var request = new RestRequest(Method.GET);
    IRestResponse response = client.Execute(request);
    Console.WriteLine(response.Content);
    

    you could use HttpClient class also, if you want to use WebClient you could do it with WebClient.DownloadString Method while I don't recommend using WebClient for this scraping, keep in mind Instagram may block you if blocked you , you need residential proxies to bypass the block.

    the response will be json data , use Json.Net or similar library to deserialize it.

    just replace instagram with any username you want in the given url.