2017-05-29 58 views
0

我有這樣的代碼:獲取一個HTML頁面中的網址與HTML敏捷性包

foreach (HtmlNode node in hd.DocumentNode.SelectNodes("//div[@class='compTitle options-toggle']//a")) 
    { 
     string s=("node:" + node.GetAttributeValue("href", string.Empty)); 
    } 

我想要得到的URL在標籤是這樣的:

<div class="compTitle options-toggle"> 

    <a class=" ac-algo fz-l ac-21th lh-24" href="http://www.bestbuy.com"> 
       <b>Huawei</b> Products - Best Buy 
    </a> 
</div> 

我想「http://www.bestbuy.com」和「華爲產品 - 百思買」

我該怎麼辦?我的代碼是否正確?

+0

'是我的代碼正確' - 爲什麼你不能檢查是否你的代碼是正確的? –

+0

它不返回我的網址 – mary

+1

那麼你應該知道問題的答案*「我的代碼是否正確?」* –

回答

1

這是工作的代碼

 var document = new HtmlDocument(); 
     document.LoadHtml("<div class=\"compTitle options-toggle\"><a class=\" ac-algo fz-l ac-21th lh-24\" href=\"http://www.bestbuy.com\"><b>Huawei</b> Products - Best Buy</a></div>"); 

     var tags = document.DocumentNode.SelectNodes("//div[@class='compTitle options-toggle']//a").ToList(); 

     foreach (var tag in tags) 
     { 
      var link = tag.Attributes["href"].Value; // http://www.bestbuy.com 
      var text = tag.InnerText; // Huawei Products - Best Buy 
     } 
1

閉幕雙引號應該解決的選擇(它的工作對我來說)的例子。

獲得純文本

string contentText = node.InnerText; 

或具有粗體字華爲,是這樣的:

string contentHtml = node.InnerHtml;