使用HtmlAgilitypack问题解析表

时间:2015-02-01 23:08:14

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

我正在尝试解析一个看起来像这样的表:                                                            

<table><tbody>
<tr><th a href=""></th><th></th></tr>
<tr><td class="v"></td><td class="d"></td><td class="h"></td><td class="a">   </td><td class="o"></td><td class="o"></td><td class="o"></td><td class="p"><table class="p" title="ttt"></table></td></tr>
<tr><td class="v"></td><td class="d"></td><td class="h"></td><td class="a">   </td><td class="o"></td><td class="o"></td><td class="o"></td><td class="p"><table class="p" title="eee"></table></td></tr>
<tr><td class="v"></td><td class="d"></td><td class="h"></td><td class="a">   </td><td class="o"></td><td class="o"></td><td class="o"></td><td class="p"><table class="p" title="rtr"></table></td></tr>
<tr><th a href=""></th><th></th></tr>
<tr><td class="v"></td><td class="d"></td><td class="h"></td><td class="a">   </td><td class="o"></td><td class="o"></td><td class="o"></td><td class="p"><table class="p" title="ouu"></table></td></tr>
<tr><td class="v"></td><td class="d"></td><td class="h"></td><td class="a">   </td><td class="o"></td><td class="o"></td><td class="o"></td><td class="p"><table class="p" title="teee"></table></td></tr>
</tbody></table>

我在ASP.net中使用此代码来获取我想要的每一行中的单元格:

var getHtmlWeb = new HtmlWeb();
var document = getHtmlWeb.Load(txtbox.Text); 
//get tables
foreach (HtmlNode table in document.DocumentNode.SelectNodes("//table"))
        {
            //get each table row
            foreach (HtmlNode row in table.SelectNodes("tr"))
            {

                Outputlabel.Text += "row: <br />";
//get table head tags that have a link, get the Inner text
                if((row.SelectSingleNode("//th//a").InnerText) != null)
                {

                    Outputlabel.Text += row.SelectSingleNode("//th//a").InnerText + "<br />";
                }
                // get the cells with the classes I want
                    string d = row.SelectSingleNode("//td[@class='d']").InnerText;
                    Outputlabel.Text += row.SelectSingleNode("//td[@class='d']").InnerText + " ";

                    string h = row.SelectSingleNode("//td[@class='h']").InnerText;
                    Outputlabel.Text += row.SelectSingleNode("//td[@class='h']").InnerText + " ";
                    string a = row.SelectSingleNode("//td[@class='a']").InnerText;
                    Outputlabel.Text += row.SelectSingleNode("//td[@class='a']").InnerText + " ";
                    string op = "";
//there are 3 classes in each row to have the class="o"
                    if (row.SelectNodes("//td[@class='o']") != null)
                    {
                        foreach (HtmlNode o in row.SelectNodes("//td[@class='o']"))
                        {
                            op += o.InnerText;
                        }
                        Outputlabel.Text += op + " ";
                    }

                    var pr = row.SelectSingleNode("//td//table[@class='p']");
                    string pr = probability.Attributes["title"].Value;

                    Outputlabel.Text += pr + "<br />";
            }
        }  

我只获得了第一个表的第一行,并且重复多次...而且我没有得到类“o”和td标记中带有类“p”的表的标题“ p“

2 个答案:

答案 0 :(得分:0)

        public static void Main(string[] args)
        {
            string html = @"<!DOCTYPE html>
<html>
<body>


<table>
   <tbody>
      <tr>
         <th a href="""">a</th>
         <th>d2</th>
      </tr>
      <tr>
         <td class=""v"">b</td>
         <td class=""d"">v</td>
         <td class=""h"">g</td>
         <td class=""a"">  d </td>
         <td class=""o"">e</td>
         <td class=""o"">j</td>
         <td class=""o"">z</td>
         <td class=""p"">
            <table class=""p"" title=""ttt""></table>
         </td>
      </tr>
      <tr>
         <td class=""v"">1</td>
         <td class=""d"">2</td>
         <td class=""h"">3</td>
         <td class=""a""> 4  </td>
         <td class=""o"">5</td>
         <td class=""o"">6</td>
         <td class=""o"">7</td>
         <td class=""p"">
            <table class=""p"" title=""eee""></table>
         </td>
      </tr>
      <tr>
         <td class=""v"">a1</td>
         <td class=""d"">b2</td>
         <td class=""h"">v3</td>
         <td class=""a"">  d4 </td>
         <td class=""o"">e5</td>
         <td class=""o"">j6</td>
         <td class=""o"">z7</td>
         <td class=""p"">
            <table class=""p"" title=""rtr""></table>
         </td>
      </tr>
      <tr>
         <th a href="""">23</th>
         <th>qaaa</th>
      </tr>
      <tr>
         <td class=""v"">az</td>
         <td class=""d"">b2</td>
         <td class=""h"">a b</td>
         <td class=""a""> aaaa  </td>
         <td class=""o"">aa</td>
         <td class=""o"">a</td>
         <td class=""o"">aaa</td>
         <td class=""p"">
            <table class=""p"" title=""ouu""></table>
         </td>
      </tr>
      <tr>
         <td class=""v"">z</td>
         <td class=""d"">zz</td>
         <td class=""h"">zzz</td>
         <td class=""a""> zzzz  </td>
         <td class=""o"">zzzzzz</td>
         <td class=""o"">zzzzz</td>
         <td class=""o"">zzzzzzz</td>
         <td class=""p"">
            <table class=""p"" title=""teee""></table>
         </td>
      </tr>
   </tbody>
</table></body>
</html>";

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


            var coll = doc.DocumentNode.Descendants().Where(x => x.Name == "th" || 
                (x.Name == "td" && 
                    (x.Attributes["class"].Value == "v" || 
                     x.Attributes["class"].Value == "d" ||
                     x.Attributes["class"].Value == "h" ||
                     x.Attributes["class"].Value == "a" ||
                     x.Attributes["class"].Value == "o")) ||
                (x.Name=="table" && x.Attributes["class"] != null &&  x.Attributes["class"].Value == "p")).ToList();

            foreach (HtmlNode node in coll)
            {
                if(node.Name=="table")
                {
                    Console.WriteLine(node.Attributes["title"].Value);
                }
                else
                {
                    Console.WriteLine(node.InnerText.Trim());
                }
            }  
        }

你应该采取特定的元素而不是整个表格。以下代码采用th,td InnerText的所有table并返回class="p"的{​​{1}}标题。

以后请格式化您的HTML!

这是你逻辑的关键部分。我没有选择td class="p",因为您只从表标题中获取此元素。

var coll = doc.DocumentNode.Descendants().Where(x => x.Name == "th" || 
                (x.Name == "td" && 
                    (x.Attributes["class"].Value == "v" || 
                     x.Attributes["class"].Value == "d" ||
                     x.Attributes["class"].Value == "h" ||
                     x.Attributes["class"].Value == "a" ||
                     x.Attributes["class"].Value == "o")) ||
                (x.Name=="table" && x.Attributes["class"] != null &&  x.Attributes["class"].Value == "p")).ToList();

答案 1 :(得分:0)

它似乎以这种方式用于在线html文件:

    HtmlWeb getHtmlWeb = new HtmlWeb();

        HtmlDocument doc = getHtmlWeb.Load(txtbox.Text);

        string d = "//td[@class='d']";
        string h = "//td[@class='h']";
        string a = "//td[@class='a']";
        string p = "//table[@class='p']";


        HtmlNodeCollection ds = doc.DocumentNode.SelectNodes(d);
        HtmlNodeCollection hs = doc.DocumentNode.SelectNodes(h);
        HtmlNodeCollection as = doc.DocumentNode.SelectNodes(a);
        HtmlNodeCollection ps = doc.DocumentNode.SelectNodes(p);
foreach (HtmlNode n in ds)
        {
            Outputlabel.Text += n.InnerHtml + "<br />"; 
        }

        foreach (HtmlNode h in hs)
        {
            Outputlabel.Text += h.InnerHtml + "<br />";
        }
        foreach (HtmlNode a in as)
        {
            Outputlabel.Text += a.Attributes["href"].Value + "<br />";
        }
        foreach (HtmlNode p in ps)
        {
            Outputlabel.Text += p.Attributes["title"].Value + "<br />";
        }