C# Downloading Instagram Profile As HTML

c# download html html-agility-pack instagram

Question

I have been trying to download an public Instagram profile to the fetch stats such as followers and bio. I have been doing this in a c# console application and downloading the HTML using HTML Agility Pack.

Code:

string url = @"https://www.instagram.com/" + Console.ReadLine() + @"/?hl=en";
Console.WriteLine();

HtmlWeb web = new HtmlWeb();
HtmlDocument document = web.Load(url);
document.Save(path1);

When I save it though all I get is a bunch of scripts and a blank screen:

enter image description here

I was wondering how to save the html once all the scripts had run and formed the content

Accepted Answer

ANSWER

Thanks for the suggestions on how to download the HTML! I managed to return some instagram information in the end. Here is the code:

//(This was done using HTML Agility Pack)

string url = @"https://www.instagram.com/" + Console.ReadLine() + @"/?hl=en";

HtmlWeb web = new HtmlWeb();
HtmlDocument document = web.Load(url);

var metas = document.DocumentNode.Descendants("meta");
var followers = metas.FirstOrDefault(_ => _.HasProperty("name", "description"));

if (followers == null) { Console.WriteLine("Sorry, Can't Find Profile :("); return; }

var content = followers.Attributes["content"].Value.StopAt('-');

Console.WriteLine(content);

And HasProperty() & StopAt()

public static bool HasProperty(this HtmlNode node, string property, params string[] valueArray)
{
    var propertyValue = node.GetAttributeValue(property, "");
    var propertyValues = propertyValue.Split(' ');
    return valueArray.All(c => propertyValues.Contains(c));
}

public static string StopAt(this string input, char stopAt)
{
    int x = input.IndexOf(stopAt);
    return input.Substring(0, x);
}

NOTE:

However this is still not the answer I am looking for. I still have a wreck of HTML which is not structred the same as the HTML I recieve when I look at it in Google Chrome. Doing some searching in the HTML I managed to scalvage the content-less html for a meta tag which contains the content. This is okay for this but if I going to continue this method of finding HTML content then it may not be the same :(


Popular Answer

    public MainWindow()
    {
        InitializeComponent();
        WB_1.Navigate(@"https://www.instagram.com/" + Console.ReadLine() + @"/?hl=en");

        WB_1.LoadCompleted += wb_LoadCompleted;
    }

    void wb_LoadCompleted(object sender, NavigationEventArgs e)
    {
        dynamic doc = WB_1.Document;
        string htmlText = doc.documentElement.InnerHtml;
    }


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why