HTMLAgilityPack按类排除

时间:2015-10-14 15:39:24

标签: c# html linq html-agility-pack

我有一个包含3种不同类型的儿童的UI元素,我想在他们的班级中排除其中2种类型的儿童。使用class = map和class = map-button

排除li元素

这就是我在摆弄的东西,

HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(Constants.New_York.GetOffenderPageById(""+ 41586));

var querylist3 = from list in 
                   doc.DocumentNode.SelectNodes("//ul[@class='address label-value']")
                  .Cast<HtmlNode>()
  from row in list.SelectNodes("not(li[@class='map']) and not(li[@class='map-button'])")
                  .Cast<HtmlNode>()
  select new { CellText = Utilities.RemoveHtmlCharacters(row.InnerText) };

foreach (var q1 in querylist3)
        Console.WriteLine(q1.CellText);

所以我看着HtmlAgilityPack SelectNodes expression to ignore an element with a certain attribute,但要么我无法正确理解解决方案,要么它无效。

我不想创建对象实例并与linq进行比较...

作为参考,这是HTML,

<ul class="address label-value">
   <li><span class="label">Type</span><span class="value">RES&nbsp;(Primary)</span></li>
   <li><span class="label">County</span><span class="value">Suffolk</span></li>
   <li class="no-border-bottom"><span class="label">Address</span><span class="value">1010 OLD MEDFORD RD<br>FARMINGVILLE,&nbsp;New York&nbsp;11738</span></li>
   <li class="map-button"><a class="cta cta-blue cta-small" href="https://www.google.com/maps/embed/v1/place?key=AIzaSyDLQ6Qf1v0RJyudkTTEcAYsXPIewwO4F3w&amp;zoom=17&amp;q=1010 OLD MEDFORD RD,FARMINGVILLE,NY,11738">View on Map</a></li>
   <li class="map">
      <iframe src="" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" class="map-iframe">&lt;!----&gt;</iframe>
      <p><a href="../nsor/mapping-exceptions.htm">Learn about possible mapping exceptions.</a></p>
   </li>
</ul>

1 个答案:

答案 0 :(得分:0)

我的答案不是我希望学习的东西......但它解决了它。我仍然想知道如何在htmlnode select ...

的搜索参数中使用

我暂时使用linq ...不确定哪个更好或什么......

 var querylist3 = from list in doc.DocumentNode.SelectNodes("//ul[@class='address label-value']").Cast<HtmlNode>()
                     from row in list.SelectNodes("li").Where(x => !x.Attributes.Contains("class") || !x.Attributes["class"].Value.Contains("map")).Cast<HtmlNode>()
                     select new { CellText = Utilities.RemoveHtmlCharacters(row.InnerText) };