2011-09-02 54 views
1

解析,我有以下來源一小段代碼片段:問題查詢網頁的HTML敏捷性包

<div class = "discount_tools_row"> 
    <div class = "discount_tools"> 
    <ul> 
     <li><a href = "#" class = "share-discount" rel = "nofollow"></a></li> 
     <li><a href = "/deal/map/4243683" 
      class = "show-location" 
      title = "הראה מקום על מפה" 
      data-address = "רח&#39; האצ&quot;ל 39, ראשון לציון"></a></li> 
    </ul> 

    <link rel = "prerender" 
      href = "http://www.bigdeal.co.il/? CampaignId = 873 & sId = 10 "> 
    <a class = "tavo_button" 
     data-provider = "bigdeal" 
     href = "http : //www.bigdeal.co.il/?CampaignId=873&sId=10" 
     target="_blank" 
     rel = "nofollow">תבוא!</a> 
    </div> 
    </div> 
</div> 

使用HTML敏捷性包我希望獲取對<data-address value, link rel="prerender" href value>

我嘗試以下,但得到錯誤的結果:

var nodes = doc.DocumentNode.SelectNodes(
    "//div[@class=\"discount_tools\"]"); 
var geoNodes = nodes.Where(node => !string.IsNullOrEmpty(
    node.ChildAttributes("data-address").ToString())); 
AnswerFormat ans = new AnswerFormat { 
    Locations = geoNodes.Select(
     node => node.ChildAttributes("data-address").ToString()).ToList(), 
    //Names = nodes.Select(node => node.Attributes["data-address"].Value). 
    //ToList(), 
    Details = geoNodes.Select(
     node => node.ChildAttributes("data-direct-url").ToString()).ToList() 
}; 

我試圖實現所有

< div class = "discount_tools" > 

與thier childNode

data-address 

屬性和

<a class="tavo_button" data-provider="bigdeal" href= 

在另一個孩子節點

如何改善我的查詢?

回答

0

這是我的解決方案:

 var nodes = doc.DocumentNode.SelectNodes("//div[@class=\"discount_tools\"]"); 
     var linksCollections = nodes.Select(node => node.Descendants("a")); 

     List<string> Locations = new List<string>(); 
     List<string> Categories = new List<string>(); 
     List<string> Hrefs = new List<string>(); 

     foreach (var col in linksCollections) 
     { 
      string location, category, href; 
      location = GetAtt("data-address",col); 
      if (!string.IsNullOrEmpty(location)) 
      { 
       category = GetAtt("data-kind", col); 
       if (!string.IsNullOrEmpty(category)) 
       { 
        href = GetAtt("data-provider", "href", col); 
        if (!string.IsNullOrEmpty(href)) 
        { 
         Locations.Add(location); 
         Categories.Add(category); 
         Hrefs.Add(href); 
        } 
       } 
      } 

     } 
0
String dataAddressValue = doc.DocumentNode.SelectSingleNode("//div[@class='discount_tools']/ul/li/a[@class='show-location']").Attributes["data-address"].Value; 

String LinkHrefValue = doc.DocumentNode.SelectSingleNode("//div[@class='discount_tools ']/link[@rel=’prerender’]").Attributes["href"].Value;