-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!

Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
sceru
  • 80
  • 1
  • 2
  • 10
  • 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. – Rup Dec 12 '18 at 11:53
  • 7
    Or [there are YouTube APIs for this](https://stackoverflow.com/q/30723866/243245) rather than having to go to a third-party site anyway. – Rup Dec 12 '18 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. – Robin Bennett Dec 12 '18 at 11:54
  • already answered https://stackoverflow.com/questions/11426204/extract-content-from-html-page you need to refine you search/ how to search/ search before question – code.rider Dec 12 '18 at 12:08

1 Answers1

-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);
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
  • That works for me! Thank you. – sceru Dec 12 '18 at 12:04
  • 1
    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? – ProgrammingLlama Dec 12 '18 at 12:08
  • 1
    @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 Dec 12 '18 at 12:10
  • @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. – ProgrammingLlama Dec 12 '18 at 12:10
  • 2
    It is effectively Step 2 of the OP's overall problem - alas their question focuses only on Step 1 @John. :( – mjwills Dec 12 '18 at 12:11
  • @aliozgurr if you think that this solution works for you. Please consider to tick the green box near answer. – Derviş Kayımbaşıoğlu Dec 12 '18 at 12:17
  • `I believe that he is retrieving JSON result` He definitely isn't @Simonare. Go `View Source` on the URL the OP mentions. It is definitely, 100% HTML. – mjwills Dec 12 '18 at 12:51