HtmlAgilityPack 有属性吗?

发布于 2024-09-30 11:33:49 字数 246 浏览 6 评论 0原文

我想做的是

node.Attributes["class"].Value

但是如果节点没有 class 属性,它就会崩溃。所以,我必须先检查它是否存在,对吗?我该怎么做? Attributes 不是一个字典(它是一个包含内部字典的列表??),并且没有 HasAttribute 方法(只是一个 HasAttributes 指示它是否有任何属性)。我该怎么办?

All I want to do is

node.Attributes["class"].Value

But if the node doesn't have the class attribute, it crashes. So, I have to check for its existence first, right? How do I do that? Attributes is not a dict (its a list that contains an internal dict??), and there's no HasAttribute method (just a HasAttributes which indicates if it has any attribute at all). What do I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感受沵的脚步 2024-10-07 11:33:49

更新答案

如果属性丢失,请使用 node.Attributes["class"]?.Value 返回 null。这与下面的 ValueOrDefault() 相同。

原始答案

试试这个:

String val;
if(node.Attributes["class"] != null)
{
  val = node.Attributes["class"].Value;
}

或者你可以添加这个

public static class HtmlAgilityExtender
{
    public static String ValueOrDefault(this HtmlAttribute attr)
    {
        return (attr != null) ? attr.Value : String.Empty;
    }
}

然后使用

node.Attributes["class"].ValueOrDefault();

我还没有测试过那个,但它应该可以工作。

Updated answer

Use node.Attributes["class"]?.Value to return null if the attribute is missing. This will be the same as the ValueOrDefault() below.

Original answer

Try this:

String val;
if(node.Attributes["class"] != null)
{
  val = node.Attributes["class"].Value;
}

Or you might be able to add this

public static class HtmlAgilityExtender
{
    public static String ValueOrDefault(this HtmlAttribute attr)
    {
        return (attr != null) ? attr.Value : String.Empty;
    }
}

And then use

node.Attributes["class"].ValueOrDefault();

I havent tested that one, but it should work.

走过海棠暮 2024-10-07 11:33:49

请尝试这个:

String abc = String.Empty;     
      if (tag.Attributes.Contains(@"type"))
      {
          abc = tag.Attributes[@"type"].Value;
      }

Please try this:

String abc = String.Empty;     
      if (tag.Attributes.Contains(@"type"))
      {
          abc = tag.Attributes[@"type"].Value;
      }
寂寞美少年 2024-10-07 11:33:49

此代码可用于获取两个脚本标记之间的所有文本。

String getURL(){
return @"some site address";
}
List<string> Internalscripts()
    {
        HtmlAgilityPack.HtmlDocument doc = new HtmlWeb().Load((@"" + getURL()));
        //Getting All the JavaScript in List
        HtmlNodeCollection javascripts = doc.DocumentNode.SelectNodes("//script");
        List<string> scriptTags = new List<string>();
        foreach (HtmlNode script in javascripts)
        {
            if(!script.Attributes.Contains(@"src"))
            {
                scriptTags.Add(script.InnerHtml);
            }
        }
        return scriptTags;
    }

This Code Can Be Used to get all text between two script tags.

String getURL(){
return @"some site address";
}
List<string> Internalscripts()
    {
        HtmlAgilityPack.HtmlDocument doc = new HtmlWeb().Load((@"" + getURL()));
        //Getting All the JavaScript in List
        HtmlNodeCollection javascripts = doc.DocumentNode.SelectNodes("//script");
        List<string> scriptTags = new List<string>();
        foreach (HtmlNode script in javascripts)
        {
            if(!script.Attributes.Contains(@"src"))
            {
                scriptTags.Add(script.InnerHtml);
            }
        }
        return scriptTags;
    }
倾`听者〃 2024-10-07 11:33:49

HTML Agility Pack 能够检查节点是否具有特定类或提供所有类的列表。

    //Select nodes example, get all <p> tag nodes and check if they have a class and if they do, get the class attribute.
foreach (HtmlNode node in htmlAgilityDocument.DocumentNode.SelectNodes("//p"))
{
    if (node.HasClass("ClassName"))
    {
        HtmlAttribute classAttributes = node.Attributes["ClassName"];
        //Do something ...
    }
}

//Select nodes example, get all <p> tag nodes having a specified class name.
string className = "class";
foreach (HtmlNode node in htmlAgilityDocument.DocumentNode.SelectNodes("//p[@class='" + className + "']"))
{
    //Access via class attribute
    HtmlAttribute classAttribute = node.Attributes[className];
    //Do something ...
}

//Get all class names to check for a class
bool containsClass = htmlAgilityDocument.DocumentNode.GetClasses().Contains("ClassName");
if (containsClass == true)
{
    //Do something ...
}

HTML Agility Pack has the capability to check if a node has a particular class or provide a list of all classes.

    //Select nodes example, get all <p> tag nodes and check if they have a class and if they do, get the class attribute.
foreach (HtmlNode node in htmlAgilityDocument.DocumentNode.SelectNodes("//p"))
{
    if (node.HasClass("ClassName"))
    {
        HtmlAttribute classAttributes = node.Attributes["ClassName"];
        //Do something ...
    }
}

//Select nodes example, get all <p> tag nodes having a specified class name.
string className = "class";
foreach (HtmlNode node in htmlAgilityDocument.DocumentNode.SelectNodes("//p[@class='" + className + "']"))
{
    //Access via class attribute
    HtmlAttribute classAttribute = node.Attributes[className];
    //Do something ...
}

//Get all class names to check for a class
bool containsClass = htmlAgilityDocument.DocumentNode.GetClasses().Contains("ClassName");
if (containsClass == true)
{
    //Do something ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文