C#如何仅从网站获取一个字符串

时间:2018-12-12 11:48:54

标签: c# html-agility-pack

我正在尝试从网站获取数据。但是HttpWebRequest带出了网站的整个HTML编码。我只想从该网站获得订阅者。

代码是:

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();
    }  
}

输出是这样,我把它缩短了。

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

我只想打印“计数”,但我不知道该怎么做。请帮忙!

1 个答案:

答案 0 :(得分:-1)

您的输出格式为json。这样您就可以解析json进行计数了。

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