好的,我很久之前就开始使用HTMLAgilityPack中的XPath查询。
我们来看看这个页面:http://health.yahoo.net/articles/healthcare/what-your-favorite-flavor-says-about-you。我想要提取的内容仅限于页面内容,其他内容不需要。
因此,我首先删除了脚本和样式标签。
Document = new HtmlDocument();
Document.LoadHtml(page);
TempString = new StringBuilder();
foreach (HtmlNode style in Document.DocumentNode.Descendants("style").ToArray())
{
style.Remove();
}
foreach (HtmlNode script in Document.DocumentNode.Descendants("script").ToArray())
{
script.Remove();
}
在那之后,我尝试使用//text()来获取所有文本节点。
foreach (HtmlTextNode node in Document.DocumentNode.SelectNodes("//text()"))
{
TempString.AppendLine(node.InnerText);
}
然而,我不仅没有得到纯文本,还得到了大量的/r /n字符。请给我一些指导。