create a scraper for instagram in csharp

To create a scraper for Instagram in C#, we can use the HtmlAgilityPack library to parse the HTML content of the Instagram page and extract the necessary data.

Here is an example of how to create a scraper that extracts the usernames of the top 10 posts in a user's Instagram profile:

main.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using HtmlAgilityPack;

class InstagramScraper
{
    static void Main(string[] args)
    {
        string username = "instagram_username";
        string url = "https://www.instagram.com/" + username;

        // Create a web request to the Instagram profile URL
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Method = "GET";

        // Get the response from the web server and read the HTML content
        HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
        StreamReader streamReader = new StreamReader(webResponse.GetResponseStream());
        string htmlContent = streamReader.ReadToEnd();
        streamReader.Close();
        webResponse.Close();

        // Load the HTML content into the HtmlAgilityPack document object model
        HtmlDocument htmlDocument = new HtmlDocument();
        htmlDocument.LoadHtml(htmlContent);

        // Find the div element that contains the top posts
        HtmlNode topPostsDiv = htmlDocument.DocumentNode.SelectSingleNode("//div[@class='v1Nh3 kIKUG  _bz0w']");

        // Loop through the top posts and extract the username
        List<string> usernames = new List<string>();
        foreach (HtmlNode topPost in topPostsDiv.ChildNodes)
        {
            HtmlNode usernameNode = topPost.SelectSingleNode(".//a[@class='sqdOP yWX7d     _8A5w5   ZIAjV ']");
            string username = usernameNode.InnerText.TrimStart('@');
            usernames.Add(username);
        }

        // Print the list of usernames
        Console.WriteLine("Top 10 usernames in " + username + "'s profile:");
        for (int i = 0; i < usernames.Count && i < 10; i++)
        {
            Console.WriteLine((i + 1) + ": " + usernames[i]);
        }
    }
}
1853 chars
49 lines

Note that this code only works for public Instagram profiles, as private profiles require authentication. Also, please be aware of Instagram's scraping policy and ensure that your use of this code complies with their terms of service.

gistlibby LogSnag