HtmlAgilityPack: get all elements by class

不打扰是莪最后的温柔 提交于 2019-12-24 17:22:00

问题


I have an HTML, and i need to get some nodes by class. So i can't do it because

  1. I dunno XML path
  2. Items needed has no ID, only class
  3. HtmlAgilityPack do not allow to get all elements (like XDocument allows), but doc.Elements() works only if i have an id, but i haven't. So i also dunno XML path so i cannot use SelectNodes method
  4. I cannot use regexps

my code was

public static class HapHelper
{
    private static HtmlNode GetByAttribute(this IEnumerable<HtmlNode> htmlNodes, string attribute, string value)
    {
        return htmlNodes.First(d => d.HasAttribute(attribute) && d.Attributes[attribute].ToString() == value);
    }

    public static HtmlNode GetElemenyByAttribute(this HtmlNode parentNode, string attribute, string value)
    {
        return GetByAttribute(parentNode.Descendants(), attribute, value);
    }

    public static bool HasAttribute(this HtmlNode d, string attribute)
    {
        return d.Attributes.Contains(attribute);
    }

    public static HtmlNode GetElementByClass(this HtmlNode parentNode, string value)
    {
        return parentNode.GetElemenyByAttribute("class", value);
    }
}

but it doesn't works, because Descendants() returns only nearest nodes.

What can I do?


回答1:


Learn XPath! :-) It's really simple, and will serve you well. In this case, what you want is:

SelectNodes("//*[@class='" + classValue + "']") ?? Enumerable.Empty<HtmlNode>();


来源:https://stackoverflow.com/questions/22974491/htmlagilitypack-get-all-elements-by-class

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