I made a simple program for fetching youtube users in comments. This is the code
string html;
using (var client = new WebClient())
{
html = client.DownloadString("http://www.youtube.com/watch?v=ER5EnjskCvE");
}
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
List<string> data = new List<string>();
HtmlNodeCollection nodeCollection = doc.DocumentNode.SelectNodes("//*[@id='comments-view']/ul[1]/li[1]/a/span/span/span/span/img");
foreach (HtmlNode node in nodeCollection)
{
data.Add(node.GetAttributeValue("alt",null));
}
But i have a problem that my nodeCollection is returning null. For the XPath i used copy XPath option in chrome under F12
html
after it's being downloaded by theWebClient
. I have just done that for you, and it is because the HTML downloaded by WebClient is just the basic Javascript that is used to then render the full page. Therefore, HtmlAgilityPack won't "see" anything past the JS. I would suggest just ditching HtmlAgilityPack altogether and using an API designed for YouTube: developers.google.com/youtube – Arran Oct 24 '13 at 11:06