Search code examples
c#html-agility-pack

C# How to get only one string from a website


I'm trying to get data from a website. But the HttpWebRequest brings out the whole HTML coding of the website. I want to get only subscribers from the website.

The code is:

using System;
using System.Net;
using System.IO;

class DownloadPageHttpWebRequest
{
static void Main()
{
    string html = string.Empty;
    string url = "https://grow.grin.co/live-youtube-subscriber-count/PewDiePie";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    using (Stream stream = response.GetResponseStream())
    using (StreamReader reader = new StreamReader(stream))
    {
        html = reader.ReadToEnd();
    }
    Console.WriteLine(html);
    Console.ReadKey();
    }  
}

The output is like that, i shortened it.

var start = {
    id: "UC-lHJZR3Gqxm24_Vd_AJ5Yw",
    count: 76239202,
    name: "PewDiePie"
    ...
}

I only want to print the 'count' but i don't know how to do it. Please help!


Solution

  • your output format is json. So you can parse your json to get count.

        var start = "{ id: 'UC-lHJZR3Gqxm24_Vd_AJ5Yw', count: 76239202, name: 'PewDiePie' }";
        dynamic result = JsonConvert.DeserializeObject(start);
        var count = result.count;
        Console.WriteLine(count);