HTML <p>节点InnerText包括CsQuery中的锚文本</p>

时间:2014-03-11 02:01:09

标签: csquery

我正在使用CsQuery解析一些wordpress博客文章,对它们进行一些文本聚类分析。我想从相关的<p>节点中删除文本。

var content = dom["div.entry-content>p"];
if (content.Length == 1)
{
    System.Diagnostics.Debug.WriteLine(content[0].InnerHTML);
    System.Diagnostics.Debug.WriteLine(content[0].InnerText);
}

在其中一篇帖子中,InnerHTML看起来像这样:

An MIT Europe project that attempts to <a title="Wired News: Gizmo Puts Cards 
on the Table" href="http://www.wired.com/news/technology/0,1282,61265,00.html?
tw=rss.TEK">connect two loved ones seperated by distance</a> through the use 
of two tables, a bunch of RFID tags and a couple of projectors.

和相应的InnerText一样

  

麻省理工学院欧洲项目试图通过使用两个表,   一堆RFID标签和几台投影机。

即。内部文本缺少锚文本。我可以自己解析HTML,但我希望有一种方法让CsQuery给我

  

麻省理工学院欧洲项目试图连接两个亲人   通过使用两张桌子,一堆RFID,按距离分开   标签和一些投影仪。

(我的斜体。)我应该怎么做到这一点?

2 个答案:

答案 0 :(得分:4)

   string result = dom["div.entry-content>p"].Text();

文本功能将包括以下所有内容,包括p标签。

答案 1 :(得分:1)

尝试使用HtmlAgilityPack

using HAP = HtmlAgilityPack;
...
var doc = new HAP.HtmlDocument();
doc.LoadHtml("Your html");
var node = doc.DocumentNode.SelectSingleNode(@"node xPath");
Console.WriteLine(node.InnerText());

xPath是页面上节点的路径。

例如:在Google Chrome中,按F12并选择您的节点,右键单击并选择“复制xPath”

此主题标题xPath:// * [@ id =“question-header”] / h1 / a

相关问题