Html Agilitypack获取外部项目

时间:2015-05-09 05:36:22

标签: asp.net web-crawler html-agility-pack

我需要<div class="month">May 2015</div><div class="author">Alex Xander</div> id="duyuru3483"。我怎样才能达到上部div(XPATH代码):

<div class="item">
<div class="dateBox">
   <div class="month">May 2015</div>
   <div class="day">07</div>
</div>
<div class="mainInfo">
<div class="title">
   <a href="#" class="text-justify" data-toggle="modal" data-target="#duyuru3483">
   2014-2015 Yılı Mezuniyet Töreni Hk. 
   </a>
   <div class="modal fade" id="duyuru3483" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
         <div class="modal-content">
            <div class="modal-header" style="background-color: #009E49;">
               <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
               <h4 class="modal-title" id="myModalLabel">
                  <div class="duyuruBaslik">2014-2015 Yılı Mezuniyet Töreni Hk.</div>
               </h4>
            </div>
            <div class="modal-body">
               <div class="duyuruMetni"> Mezuniyet törenine katılmak için yapılmasıgereken işlemler ve doldurulması gereken form ektedir. </div>
               <hr>
               <div class="author" > Ek :  <a style="color:##428bca" href="../files/19_mezuniyet.rar">indir</a>  </div>
            </div>
            <div class="modal-footer">
               <button type="button" class="btn btn-default" data-dismiss="modal">Kapat</button>
            </div>
         </div>
      </div>
   </div>
</div>
<div class="author">Alex Xander</div>

1 个答案:

答案 0 :(得分:2)

一种选择是使用Ancestors

var modal = doc.GetElementbyId("duyuru3483");
var parent = modal.Ancestors()
                  .Single(e => e.Attributes["class"] !=null && 
                               e.Attributes["class"].Value == "item");

另一种选择是使用XPath:

var parent = doc.DocumentNode
                .SelectSingleNode("div[@class='item'][//div[@id='duyuru3483']]");

(您可以使用以下方式直接转到.dateBox

div[@class='item'][//div[@id='duyuru3483']]/div[@class='dateBox']

当然,你可以使用ParentNode,但这不太健壮:

var parent = modal.ParentNode.ParentNode.ParentNode;

所有这些都可以让您.item - 从那里获取数据应该很容易。