in my UWP application i need to take the title and the link of the first video in a youtube page by HtmlAgilityPack, but the result is the following, and the value of title and href isn't show.
id="video-title"
class="yt-simple-endpoint style-scope ytd-video-renderer"
aria-label$="[[data.title.accessibility.accessibilityData.label]]"
href$="[[computeHref_(data.navigationEndpoint)]]"
title$="[[getSimpleString(data.title)]]"
data="[[data.navigationEndpoint]]">
This is the code
var html = @"https://www.youtube.com/results?search_query=cado+dalle+nubi+trailer+ita";
HtmlWeb web = new HtmlWeb();
HtmlDocument htmlDoc = web.Load(html);
var node = htmlDoc.DocumentNode.SelectSingleNode("//div/div/div/div/h3/a");
string result = node.OuterHtml;
Use WebView
instead of HtmlWeb
to get the HTML
WebView webView = new WebView();
webView.Navigate(new Uri(@"https://www.youtube.com/results?search_query=cado+dalle+nubi+trailer+ita"));
private async void WebView_NavigationCompletedAsync(WebView sender, WebViewNavigationCompletedEventArgs args)
{
var siteHtML = await webView.InvokeScriptAsync("eval", new string[] { "document.documentElement.innerHTML;" });
}
The first video was in the second index. So use SelectNodes
and select the second index
HtmlDocument htmlDoc = web.Load(siteHtML );
string result = htmlDoc.DocumentNode.SelectNodes("//div/div/div/div/h3/a")[1].OuterHtml;
Here is the final code
WebView webView = new WebView();
public void GetFirstVideo(string UrlString)
{
webView.Navigate(new Uri(UrlString));
webView.NavigationCompleted -= WebView_NavigationCompletedAsync; //To avoid multiple subscribe
webView.NavigationCompleted += WebView_NavigationCompletedAsync;
}
private async void WebView_NavigationCompletedAsync(WebView sender, WebViewNavigationCompletedEventArgs args)
{
webView.NavigationCompleted -= WebView_NavigationCompletedAsync; //To stop if there is any re-direct
var siteHtML = await webView.InvokeScriptAsync("eval", new string[] { "document.documentElement.innerHTML;" });
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(siteHtML);
var firstVideoTitle = htmlDocument.DocumentNode.SelectNodes("//div/div/div/div/h3/a")[1].OuterHtml;
}