通过HtmlAgilityPack获取属性的值

时间:2017-01-16 01:44:35

标签: c# html-agility-pack

<div class="vcard-names-container py-3 js-sticky js-user-profile-sticky-fields " style="position: static;">
  <h1 class="vcard-names">
    <span class="vcard-fullname d-block" itemprop="name">Name 001</span>
    <span class="vcard-username d-block" itemprop="additionalName">Name 002</span>
  </h1>
</div>

您好,我想知道如何检索此结构的名称('Name 001'和'Name 002')我做了几次测试,但我无法解析这些值,我该如何实现?

(注意:抱歉我的英语不好)

2 个答案:

答案 0 :(得分:0)

您可以使用XPath查询文档节点以查找您要查找的节点:

static void Main(string[] args)
{
    var html = @"<div class=""vcard - names - container py - 3 js - sticky js - user - profile - sticky - fields "" style=""position: static; "">
           < h1 class=""vcard-names"">
            <span class=""vcard-fullname d-block"" itemprop=""name"">Name 001</span>
            <span class=""vcard-username d-block"" itemprop=""additionalName"">Name 002</span>
          </h1>
        </div>";

    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
    doc.LoadHtml(html);

    var names = doc.DocumentNode.SelectNodes("//span").Select(x => x.InnerText);
    foreach (var name in names)
    {
        Console.WriteLine(name);
    }
    Console.ReadLine();
}

答案 1 :(得分:0)

这可能会为你做到这一点

HtmlDocument doc = new HtmlDocument();
doc.Load(YourHTMLFile);
foreach(HtmlNode Spans in doc.DocumentNode.SelectNodes("//span"))
{
    Console.WriteLine(Spans.InnerText);
}