I will leave an answer that meets your need, but using another approach. If you need to read HTML content from a Web page, you will have a hard time doing this using the WebBroser control, especially outside of a Windows Forms application. I've been able to accomplish what you need by using the System.Net.WebClient
class and the HTML Agility Pack library . Just change the value of the nomeDoCanal
variable to the channel you want to read the data to:
HtmlDocument doc = new HtmlDocument();
WebClient client = new WebClient();
string nomeDoCanal = "FOO";
string source = client.DownloadString(String.Format("https://www.youtube.com/user/{0}/about", nomeDoCanal));
doc.LoadHtml(source);
HtmlNode node = doc.DocumentNode.Descendants("ul").FirstOrDefault(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("about-stats"));
HtmlNode[] itens = node.Descendants("li").ToArray();
HtmlNode liSubscribers = itens[0];
HtmlNode liViews = itens[1];
string subscribers = liSubscribers.Element("b").InnerText;
string views = liViews.Element("b").InnerText;
Console.WriteLine("Subscribers: {0}", subscribers);
Console.WriteLine("Views: {0}", views);
Console.ReadKey();
To get the HTML Agility Pack library using nuget:
PM> install-package htmlagilitypack
If you can not use Nuget, you can download a version here (link Release Binaries):
link
Switch 1.5.5 to the desired version, on the nuget page link , on the right side, there is a link called "manual download"
The relevant HTML excerpt from the YouTube page is as follows. Keep in mind that any Youtube layout change will make this program stop working:
<ul class="about-stats">
<li class="about-stat ">
<b>312,992</b> subscribers
</li>
<li class="about-stat ">
<b>71,629,148</b> views
</li>
Library site link:
link