C# html agility pack get elements by class name

时光毁灭记忆、已成空白 提交于 2020-01-10 02:50:06

问题


I'm trying to get all the divs that their class contains a certain word:

<div class="hello mike">content1</div>
<div class="hello jeff>content2</div>
<div class="john">content3</div>

I need to get all the divs that their class contains the word "hello". Something like this:

resultContent.DocumentNode.SelectNodes("//div[@class='hello']"))

how can i do it with agility pack?


回答1:


I got it:

resultContent.DocumentNode.SelectNodes("//div[contains(@class, 'hello')]"))



回答2:


I'm sure because there're multiple classes in your div, that doesn't work. You can try this instead:

resultContent.DocumentNode.Descendants("div").Where(d => d.Attributes["class"].Value.Contains("hello"));



回答3:


As I wrote here, as of version v1.6.5 of Html Agility Pack, it contains .HasClass("class-name") extension methoda.

IEnumerable<HtmlNode> nodes =
    htmlDoc.DocumentNode.Descendants(0)
        .Where(n => n.HasClass("class-name"));



回答4:


as you have specified that the class has to contain a certain word, the following will ensure that the word is:

  • at the start of the string and followed by a space
  • or in the middle of the string and surrounded by whitespace
  • or at the end of the string and preceded by a space
  • or the only class name in the class attribute

It does so by comparing the value of the class attribute surrounded by spaces with the specified word (hello) surrounded by spaces. This is to avoid false positives like class="something-hello-something"

resultContent.DocumentNode.SelectNodes("//div[contains(concat(' ', @class, ' '), ' hello ')]");



回答5:


HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.Load(filePath);
 foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//div[@class='hello']")
 {
    //code
 }


来源:https://stackoverflow.com/questions/36711680/c-sharp-html-agility-pack-get-elements-by-class-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!