从h1 className获取文本

时间:2015-02-03 12:21:18

标签: c# html html-agility-pack

我试图获取以下文字' getthis'通过使用敏捷包。

<h1 class="point">getthis< span class="level">Niveau 0</span> </h1>

我试过了:

var links = webBrowser1.Document.GetElementsByTagName("point");
foreach (HtmlElement link in links)
{
    Console.WriteLine("AAAAA");
}

var varit = doc.DocumentNode
               .SelectSingleNode("//h1[@class='point']")
               .InnerText;

Console.WriteLine("WB2 TRUE ", varit.ToString());*

var varit = doc.DocumentNode
               .SelectSingleNode("//h1[@class='point']")
               .InnerHtml;

Console.WriteLine("WB2 TRUE ", varit.ToString());*

为什么我的代码没有工作?

2 个答案:

答案 0 :(得分:0)

试试这个:

htmlDoc.DocumentNode.Descendants("h1")
    .Where(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("point"))
    .First().InnerHtml;

此代码:

string html = "<h1 class=\"point\">getthis< span class=\"level\">Niveau 0</span> </h1>";
            HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
            htmlDoc.LoadHtml(html);
            var str = htmlDoc.DocumentNode.Descendants("h1").Where(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("point")).First().ChildNodes[0].InnerText;

返回:“getthis”

答案 1 :(得分:0)

我发现这个问题之后解决了这个问题:

var a = doc.DocumentNode.SelectNodes(&#34; // h1&#34;)。Skip(0).Take(1).Single();