使用 HtmlAgilityPack 显示节点内容时遇到问题
Having trouble displaying the node's content with HtmlAgilityPack
我在以下网址上进行数据抓取时遇到问题: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是这样的:
The Html code within the website. Arrows point at the node I'm trying to reach and the content within it I'm trying to display on the console
当我 运行 我在控制台上的代码检查节点及其内容并尝试显示 Xpath 的外部 Html 字符串时,控制台将这样显示给我:
Console Window Display
我希望你们中的一些人能够向我解释为什么它会这样。这两天我在google上尝试了各种搜索,试图找出问题所在,但没有用。提前谢谢大家。
您想要的内容是动态加载的。
请改用 HtmlWeb.LoadFromBrowser()
方法。此外,检查 htmlDoc
是否有 null
,而不是调用它两次。您当前的逻辑并不能保证您的状态。
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 :)
我在以下网址上进行数据抓取时遇到问题: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是这样的:
The Html code within the website. Arrows point at the node I'm trying to reach and the content within it I'm trying to display on the console
当我 运行 我在控制台上的代码检查节点及其内容并尝试显示 Xpath 的外部 Html 字符串时,控制台将这样显示给我:
Console Window Display
我希望你们中的一些人能够向我解释为什么它会这样。这两天我在google上尝试了各种搜索,试图找出问题所在,但没有用。提前谢谢大家。
您想要的内容是动态加载的。
请改用 HtmlWeb.LoadFromBrowser()
方法。此外,检查 htmlDoc
是否有 null
,而不是调用它两次。您当前的逻辑并不能保证您的状态。
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 :)