HtmlAgilityPack Youtube搜索UWP c#

c# html-agility-pack uwp web-scraping youtube

在我的UWP应用程序中,我需要通过HtmlAgilityPack获取youtube页面中第一个视频的标题和链接,但结果如下,并且不显示title和href的值。

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]]">

这是代码

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]]">

热门答案

使用WebView而不是HtmlWeb来获取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;" });
}

第一个视频是在第二个索引中。因此,使用SelectNodes并选择第二个索引

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

这是最终的代码

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



许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因