[英]Site uses Javascript and I am having trouble accessing it using htmlagilitypack
[英]Having trouble displaying the node's content with HtmlAgilityPack
我在這個 web 地址上遇到數據抓取問題: http://patorjk.com/software/taag/#p=display&f=Graffiti&t=Type%20Something%20 。
問題是:我寫了一段代碼,應該獲取某個節點的內容並將其顯示在控制台上。 但是,節點和特定節點本身的內容似乎無法訪問,但我知道它們存在的事實是我在代碼中創建了一個條件,以便讓我知道是否找到了某個主體的節點它確實被發現但由於某種原因沒有顯示:
private static void getTextArt(string font, string word)
{
HtmlWeb web = new HtmlWeb();
//cureHtml method is just meant to return the http address
HtmlDocument htmlDoc = web.Load(cureHtml(font, word));
if(web.Load(cureHtml(font, word)) != null)
Console.WriteLine("Connection Established");
else
Console.WriteLine("Connection Failed!");
var nodes = htmlDoc.DocumentNode.SelectSingleNode(nodeXpath).ChildNodes;
foreach(HtmlNode node in nodes)
{
if(node != null)
Console.WriteLine("Node Found.");
else
Console.WriteLine("Node not found!");
Console.WriteLine(node.OuterHtml);
}
}
private const string nodeXpath = "//div[@id='maincontent']";
}
網站顯示的Html是這樣的:
網站內的 Html 代碼。 箭頭指向我試圖到達的節點以及我試圖在控制台上顯示的內容
當我在控制台上運行我的代碼以檢查節點及其內容並嘗試顯示 Xpath 的 OuterHtml 字符串時,這就是控制台向我顯示它的方式:
我希望你們中的一些人能夠向我解釋為什么它會這樣。 我已經嘗試了兩天在谷歌上的各種搜索試圖找出沒有用的問題。 謝謝大家。
您想要的內容是動態加載的。
請改用HtmlWeb.LoadFromBrowser()
方法。 另外,檢查htmlDoc
的null
,而不是調用它兩次。 您當前的邏輯不能保證您的 state。
HtmlDocument htmlDoc = web.LoadFromBrowser(cureHtml(font, word));
if (htmlDoc != null)
Console.WriteLine("Connection Established");
else
Console.WriteLine("Connection Failed!");
此外,您還需要對結果進行解碼。
Console.WriteLine(WebUtility.HtmlDecode(node.OuterHtml));
如果這不起作用,那么您的cureHtml()
方法已損壞,或者您的目標是 .NET Core:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.