-5

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!

4
  • That's just search through all of the text for "count:" and extract the part up to the comma. There are plenty of ways to do this, e.g. split into lines and then .Where to find which line contains the text "count:" and process from there, or run a regular expression on the whole text - it should be easy to write something that would match and extract the number. Commented Dec 12, 2018 at 11:53
  • 7
    Or there are YouTube APIs for this rather than having to go to a third-party site anyway. Commented Dec 12, 2018 at 11:53
  • You can try parsing the string but your code may break when the web page is updated. Hopefully the web site in question will have an API that is designed to help you do this sort of thing. Commented Dec 12, 2018 at 11:54
  • already answered stackoverflow.com/questions/11426204/… you need to refine you search/ how to search/ search before question Commented Dec 12, 2018 at 12:08

1 Answer 1

-1

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);
Sign up to request clarification or add additional context in comments.

7 Comments

That works for me! Thank you.
Am I missing something here? How is it JSON? Even if I explicitly request JSON, I get HTML. I thought OP's desired output is JSON?
@John There is JSON embedded inside the HTML. I struggle to see how the OP thinks this solves their issue since their issue is how to parse the HTML, and this solution relies on knowing how to parse the HTML.
@mjwills Ah I see. It would be nice if the question made that clear. I agree, I don't understand how this is helpful to the overall solution if OP doesn't know how to parse the HTML.
It is effectively Step 2 of the OP's overall problem - alas their question focuses only on Step 1 @John. :(
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.