使用敏捷包在html doc中读取,分组和排序节点

时间:2013-03-26 17:59:54

标签: c# html html-agility-pack

我正在尝试构建一个类,该类将基于另一个网站读取,分组和排序html文档。

我将展示我现在所拥有的东西。这是一个如何构建网页的示例(请记住,它只是“如何”构建,我已经重写了整个网页):

<tr>
            <td id="ab100_ab100_ab100_Main_Sub_Sub_objComponent" class="compContainer">
                <table class="objDetails" style="position: relative; margin: auto;">
 <tr>
<div class="smallSetup" style="margin-top: 10px;">
    <b class="ft"><b></b></b>
    <div id="ab100_ab100_ab100_Main_Sub_Sub_firstProp" class="row">
       <div class="label">
           First Name:</div>
       <div class="value">
           Albert Trebla</div>
    </div>

    <div id="ab100_ab100_ab100_Main_Sub_Sub_secondProp" class="row">
        <div class="label" style="line-height:25px;">
           Second Year:</div>
        <div class="value">
           <img src="/Setup/Images.ashx?size=medium&amp;name=5&amp;type=symbol" alt="5" align="absbottom" /><img src="/Setup/Images.ashx?size=medium&amp;name=W&amp;type=symbol" alt="Second" align="absbottom" />
     </div>
     <div id="ab100_ab100_ab100_Main_Sub_Sub_thirdProp" class="row" style="height:15px; position:relative;">
         <div class="label" style="font-size:.7em;">
             Classy Stuff:</div>
         <div class="value">
             7<br /><br /></div>
     </div>

     <div id="ab100_ab100_ab100_Main_Sub_Sub_fourthProp" class="row">
         <div class="label">
             Weather:</div>
         <div class="value">
             Cloudy  — Might Rain</div>
         </div>
     <div id="ab100_ab100_ab100_Main_Sub_Sub_fifthProp" class="row">
         <div class="label">
             Front Text:</div>
         <div class="value">
             <div class="frontTextBox">Opened</div><div class="frontTextBox">The shop is opened when the bridges are lowered.</div></div>
     </div>
     <div id="ab100_ab100_ab100_Main_Sub_Sub_sixthProp" class="row">
         <div class="label">
              Flavor:</div>
         <div id="ctl00_ctl00_ctl00_MainContent_SubContent_SubContent_FlavorText" class="value">
              <div class="frontTextBox"><i>"This taste good!"</i></div></div>
     </div>

等等。

现在我在我的应用程序中构建代码的方式如下:

HtmlWeb loader = new HtmlWeb();
HtmlDocument doc = loader.Load(stringUrl);
HtmlNode parentNode = doc.GetElementById(ab100_ab100_ab100_Main_Sub_Sub_objComponent);

HtmlNodeCollection allNodes = parentNode.SelectNodes(".//div[@class='row']");

我收集了div,但我无法迈出下一步。要理解的第一件事就是那里的html代码的布局会发生变化,所以有时firstProp不会显示,有时候它是第六个prop,依此类推。

所以我要检查节点的属性是否为“label”:

foreach (HtmlNode htmlNode in allNodes)
{
    if (htmlNode.Attributes["class"].Value == "label")
    {

    }
}

但我不知道如何检查该值后,因为下一个兄弟是一个空div。我不太清楚HtmlAgilityPack是如何工作的,所以我想知道是否有更简单的方法来实现这个目标。

任何人都可以告诉我如何继续,或者我正在做什么是错的以及如何纠正它?

*编辑*

我改变了界限:

HtmlNodeCollection allNodes = parentNode.SelectNodes(".//div[@class='row']");

所以现在我的收藏范围只缩小到我得到的div。但是当我得到一个带有“label”类的div时,我仍然需要读取它,读取它的值(例如前文本),如果是正面文本,请使用类“value”获取以下div。

1 个答案:

答案 0 :(得分:2)

我建议你学习一下Html Agility Pack支持的XPATH,并允许对HTML DOM进行简明的查询。例如,以下代码:

    HtmlDocument doc = new HtmlDocument();
    doc.Load("test.htm");

    HtmlNode node = doc.GetElementbyId("ab100_ab100_ab100_Main_Sub_Sub_objComponent");
    foreach (HtmlNode row in node.SelectNodes(".//div[@class='row']"))
    {
        Console.Write(row.SelectSingleNode("div[@class='label']").InnerText.Trim());
        Console.WriteLine(row.SelectSingleNode("div[@class='value']").InnerText.Trim());
    }

将输出:

First Name:Albert Trebla
Second Year:
Classy Stuff:7
Weather:Cloudy  - Might Rain
Front Text:OpenedThe shop is opened when the bridges are lowered.
Flavor:"This taste good!"

如果您需要在值或标签div中使用HTML,那么您可以再次从那里发出XPATH查询。