【问题标题】:Use predicates in HtmlAgilityPack, Xpath在 HtmlAgilityPack、Xpath 中使用谓词
【发布时间】:2012-02-16 19:03:07
【问题描述】:

我想从网站获取数据。我正在使用 HtmlAgilityPack (C#)。在网站内容是这样的

<div id="list">
  <div class="list1">
    <a href="example1.com" class="href1" >A1</a>
    <a href="example4.com" class="href2" />
  </div>
  <div class="list2">
   <a href="example2.com" class="href1" >A2</a>
   <a href="example5.com" class="href2" />
  </div>
  <div class="list3">
   <a href="example3.com" class="href1" >A3</a>
   <a href="example6.com" class="href2" />
  </div>
  <div class="list3">
   <a href="example4.com" class="href1" >A4</a>
   <a href="example6.com" class="href2" />
  </div>
  <div class="list3">
   <a href="example5.com" class="href1" >A5</a>
   <a href="example6.com" class="href2" />
  </div><div class="list3">
   <a href="example6.com" class="href1" >A6</a>
   <a href="example6.com" class="href2" />
  </div><div class="list3">
   <a href="example3.com" class="href1" >A7</a>
   <a href="example6.com" class="href2" />
  </div>
</div>

在这里,我们有 7 个链接为 class="href1"。我只想获取 3 个链接(从第 3 个链接到第 5 个链接)。如何获取这些特定链接?

【问题讨论】:

    标签: c# asp.net xpath html-agility-pack


    【解决方案1】:

    这种代码:

        HtmlDocument doc = new HtmlDocument();
        doc.Load(myHtmlFile);
        foreach (HtmlNode node in doc.DocumentNode.SelectNodes(
            "//div[@class='list3' and position() > 2 and position() < 6]/a[@class='href1']"))
        {
            Console.WriteLine("node:" + node.InnerText);
        }
    

    会给你这个结果:

    node:A3
    node:A4
    node:A5
    

    【讨论】:

      【解决方案2】:

      您的数据似乎已经是格式良好的 XML。如果您正在解析 XHTML 页面,那么您可能会摆脱 .NET Framework 的 System.Xml 类。例如,要将数据加载到XElement,您可以使用:

      XElement xElement = XElement.Parse(@"
          <div id=""list"">
              <div class=""list1"">
                  <a href=""example1.com"" class=""href1"" >A1</a>
                  <a href=""example4.com"" class=""href2"" />
              </div>
              <div class=""list2"">
                  <a href=""example2.com"" class=""href1"" >A2</a>
                  <a href=""example5.com"" class=""href2"" />
              </div>
              <div class=""list3"">
                  <a href=""example3.com"" class=""href1"" >A3</a>
                  <a href=""example6.com"" class=""href2"" />
              </div>
              <div class=""list3"">
                  <a href=""example4.com"" class=""href1"" >A4</a>
                  <a href=""example6.com"" class=""href2"" />
              </div>
              <div class=""list3"">
                  <a href=""example5.com"" class=""href1"" >A5</a>
                  <a href=""example6.com"" class=""href2"" />
              </div>
              <div class=""list3"">
                  <a href=""example6.com"" class=""href1"" >A6</a>
                  <a href=""example6.com"" class=""href2"" />
              </div>
              <div class=""list3"">
                  <a href=""example3.com"" class=""href1"" >A7</a>
                  <a href=""example6.com"" class=""href2"" />
              </div>
          </div>");
      

      然后,要选择class 属性值为href1 的第三到第五个&lt;a&gt; 元素,请使用:

      var links = xElement.XPathSelectElements("//a[@class='href1']").Skip(2).Take(3).ToList();
      

      另一方面,如果您有一个 HtmlAgilityPack.HtmlDocument 实例,您可以使用以下命令执行 XPath 查询:

      HtmlNodeCollection links = htmlDoc.DocumentNode.SelectNodes("//a[@class='href1']");
      var links3to5 = links.Cast<HtmlNode>().Skip(2).Take(3).ToList();
      

      【讨论】:

        猜你喜欢
        • 2013-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-30
        • 1970-01-01
        • 1970-01-01
        • 2018-08-24
        • 1970-01-01
        相关资源
        最近更新 更多