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

这是代码

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;

热门答案

使用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并选择第二个索引

HtmlDocument htmlDoc = web.Load(siteHtML );
string result = htmlDoc.DocumentNode.SelectNodes("//div/div/div/div/h3/a")[1].OuterHtml;

这是最终的代码

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



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