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


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow