【问题标题】:select next node html Agility pack C#选择下一个节点 html 敏捷包 C#
【发布时间】:2013-03-04 23:50:53
【问题描述】:

我是第一次使用 htmlagilitypack 的学生。我目前正在过滤 html 以使用

获取值
foreach (HtmlNode link in bodyNode.SelectNodes("//span[@class='content-b']"))
{
    if (link.InnerText.Contains("Name"))
    {
        //MessageBox.Show("Found");
        textBox1.Text += "Name : " + ?????;
    }

    textBox1.Text += link.InnerText;
}

如您所见,我正在检查当前节点是否包含值“NAME”,如果是,我想获取下一个节点的值。如果您能帮我解决这个问题,我将非常感激。

名称的值在下一个节点 <div class='content-b'>THIS IS MY NAME</div> 中。我如何有问题地告诉 C# 在再次循环之前获取下一个节点?

谢谢。


更新:这是我正在使用的 html 代码片段,它看起来很丑,对不起,我无能为力

<span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font class="">Name of the Author: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font class="">Undertaker</font></font></span></span></span></span> <span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font class="">Name movie: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font class="">Some Movie Name</font></font></span></span></span></span> <span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font class="">Room Online: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font class="">skype123</font></font></span></span></span></span> <span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font class="">Subsites and site: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font class="">google.nl</font></font></span></span></span></span> <span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font class="">Year: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font class="">2013. </font></font></span></span></span></span> <span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font class="">Genre: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font class="">Horror</font></font></span></span></span></span> <span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font class="">Length: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font class="">00:35:45 </font></font></span></span></span></span><span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font class="">Description: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font class=""><font>Paragraph 1</font><font>Paragraph 2</font><font>Paragraph 3</font></font><br></span></span></span></span> <span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font>Video Format: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font>MP4 </font></font></span></span></span></span> <span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font>Video: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font>MPEG4 Video (H264) 720x404 29.97fps 1000Kbps </font></font></span></span></span></span><span class="post-align" style="text-align: center;"><span style="font-family: Kristen ITC;"><span style="font-size: 16px; line-height: normal;"><span style="color: #009933;"><span class="content-b"><font><font>Audio: </font></font></span></span> <span style="font-size: 12px; line-height: normal;"><span class="content-b"><font><font>AAC 44100Hz stereo 96Kbps</font></font></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

谢谢。

【问题讨论】:

  • 请添加包含两个必需节点的 HTML 示例片段。
  • @MD.Unicorn : 我添加了 HTML 片段
  • 哇!很难阅读 HTML!我会尽量简化它。
  • 它不是格式良好的 HTML。你是从那里得到的吗?它包含大量未闭合的标签、多余的闭合标签和无效的嵌套。没有&lt;div&gt; 标记是关闭的。很明显HTML Agility Pack想不通。
  • 来自社区论坛。我无法控制来源。我从循环中正确获取值,唯一的问题是在 bodynode 中获取下一个 link 的值。

标签: c# regex filtering html-agility-pack


【解决方案1】:

您正在寻找的是节点的 NextSibling 属性。在您的示例中:

foreach (HtmlNode link in bodyNode.SelectNodes("//div[@class='content-b']"))
{
    if (link.InnerText.Contains("Name"))
    {
        //MessageBox.Show("Found");
        textBox1.Text += "Name : " + link.NextSibling.InnerText;
    }

    textBox1.Text += link.InnerText;
}

【讨论】:

  • 感谢您的超快速响应,但它会产生错误:“对象引用未设置为对象的实例。”我之前也尝试过这个解决方案,但它从来没有奏效
  • 你能把你的html片段分享给两个需要的节点吗?
  • 我用我目前正在使用的 html 片段更新了帖子。
【解决方案2】:

首先,您的文档中没有class="content-b"。它们都拼写为 contet-b

其次,由于 html 格式不正确,NextSibling 将不起作用。您需要手动获取与条件匹配的下一个节点。在这种情况下,您不能使用 foreach 循环。

结果如下:

var nodes = bodyNode.SelectNodes("//div[@class='contet-b']").ToList();
for( int i =0; i < nodes.Count; i++)
{
    var link = nodes[i];
    if (link.InnerText.Contains("Name"))
    {
        textBox1.Text += "Name : ";
        if (i + 1 < nodes.Count)
        {
            // append the value of next matching `div` node
            textBox1.Text += nodes[i + 1].InnerText.Trim();
            i++; // skip this node
        }
    }
}

【讨论】:

  • 谢谢,完美运行 :-)
猜你喜欢
  • 2011-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 1970-01-01
  • 2013-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多