【发布时间】:2020-07-26 08:36:03
【问题描述】:
我在对以下网址进行数据抓取时遇到问题: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是这样的:
当我在控制台上运行代码以检查节点及其内容并尝试显示 Xpath 的 OuterHtml 字符串时,控制台将向我显示它的方式:
我希望你们中的一些人能够向我解释为什么它会这样。我已经在谷歌上尝试了两天的各种搜索,试图找出没有用的问题。提前谢谢大家。
【问题讨论】:
标签: c# html dom web-scraping html-agility-pack