使用html-agility-pack从标签中获取href值

时间:2019-08-31 15:59:09

标签: c# html-agility-pack

阅读下面的C#代码。我在这里尝试从a[@class='view-collection-link'类中获取标记href值。但是,这不起作用。它说对象未设置为对象类型错误的实例。它出什么问题了?我的意思是我想抓住这个价值-/collection/Outdoors/AMDRO-Mosquito-Control-Insecticides-and-Pesticides/Family-307952046?omsid=304755303

HTML:

<div class="plp-pod__info">   
<div class="pod-plp__description js-podclick-analytics" data-podaction="product name">
    <a class="" data-pos="0" data-request-type="sr" data-pod-type="pr" href="/p/AMDRO-Quick-Kill-32-oz-Mosquito-Yard-Spray-100530440/304755303">
    <span class="pod-plp__brand-name">AMDRO</span> 
    Some text is demo purpose
    </a>
</div>
  <a href="/collection/Outdoors/AMDRO-Mosquito-Control-Insecticides-and-Pesticides/Family-307952046?omsid=304755303" class="view-collection-link">
    <i class="view-collection-icon"></i>
    <span class="view-collection-text">View the </span>
    <span class="view-collection-title">AMDRO Mosquito Control Insecticides and Pesticides</span>
  </a>
</div>

c#:

  HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
    var href = htmlDoc.DocumentNode.SelectSingleNode("//a[@class='view-collection-link']").Attributes["href"].Value;

0 个答案:

没有答案