2017-05-29 61 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;