【问题标题】:using Htmlagility pack to find a div from html string使用 Htmlagility 包从 html 字符串中查找 div
【发布时间】:2011-10-30 17:44:15
【问题描述】:

我有一个表格,在其中的一列中,我保存了页面的所有 html。我想使用 htmlagility 从该 div 中获取一个 div(及其内容),我该怎么做。我不想从 url 加载它或进行屏幕抓取。

【问题讨论】:

    标签: asp.net asp.net-mvc-3 html-agility-pack


    【解决方案1】:
    // Load your html
    
    HtmlDocument htmlDocument = new HtmlDocument();
    htmlDocument.LoadHtml(html);
    // Find div with an id or you could use a class if you want
    var nodes = htmlDocument.DocumentNode.SelectNodes("//div[@id='myDivId']");
    

    【讨论】:

      【解决方案2】:

      我找到了这个解决方案。

       HtmlDocument doc = new HtmlDocument();
                  doc.LoadHtml(@html);
      
                  HtmlNodeCollection tableRows = doc.DocumentNode.SelectNodes("//tr");
      
                  string content = "";
                  if (tableRows.Count > 1)
                  {
                      HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[@class='account-detail']");
                     content = node.InnerHtml;
                  }
      

      感谢大家的宝贵时间。

      【讨论】:

        猜你喜欢
        • 2016-06-30
        • 2011-12-23
        • 2016-07-25
        • 1970-01-01
        • 1970-01-01
        • 2013-07-14
        • 1970-01-01
        • 1970-01-01
        • 2016-06-07
        相关资源
        最近更新 更多