使用 HTMLAgility Pack 从 HTML 中的任何位置解析上一个节点
Parsing Previous node from any where within the HTML using HTMLAgilityPack
我有以下场景。我有以下示例 html。我需要解析 this.I 我正在使用 HtmlAgilityPack 使用 VB.Net
<h1>Category 1</h1>
...
...
<h4><a>c1</a> </h4>
<h4><a>c2</a> </h4>
<h4><a>c3</a> </h4>
<h1>Category 2</h1>
...
...
<h4><a>c4</a> </h4>
<h4><a>c5</a> </h4>
<h4><a>c6</a> </h4>
<h4><a>c7</a> </h4>
<h4><a>c8</a> </h4>
<h1>Category 3</h1>
...
...
<h4><a>c9</a> </h4>
<h4><a>c10</a> </h4>
<h4><a>c11</a> </h4>
我有所有 h4 标签的列表。如何使用 HtmlAgilityPack 找到之前的 H1 标题文本?例如我应该有
For <a> c3 ..... I should have <h1> Category 1
For <a> c6 ..... I should have <h1> Category 2
For <a> c9 ..... I should have <h1> Category 3
您可以尝试寻找以前的h1。但是当缺少 1 h1 时,这会把它搞砸。
我不熟悉 VB.NET 中的 HtmlAgilityPack,所以我用 C# 编写了它。只是给你一个想法。
var headingNode = node.PreviousSibling;
while (headingNode != null && (headingNode.Name.Equals("h4") || headingNode.Name.Equals("#text")))
{
headingNode = headingNode.PreviousSibling;
}
//We will have first non h4 or #text here
我有以下场景。我有以下示例 html。我需要解析 this.I 我正在使用 HtmlAgilityPack 使用 VB.Net
<h1>Category 1</h1>
...
...
<h4><a>c1</a> </h4>
<h4><a>c2</a> </h4>
<h4><a>c3</a> </h4>
<h1>Category 2</h1>
...
...
<h4><a>c4</a> </h4>
<h4><a>c5</a> </h4>
<h4><a>c6</a> </h4>
<h4><a>c7</a> </h4>
<h4><a>c8</a> </h4>
<h1>Category 3</h1>
...
...
<h4><a>c9</a> </h4>
<h4><a>c10</a> </h4>
<h4><a>c11</a> </h4>
我有所有 h4 标签的列表。如何使用 HtmlAgilityPack 找到之前的 H1 标题文本?例如我应该有
For <a> c3 ..... I should have <h1> Category 1
For <a> c6 ..... I should have <h1> Category 2
For <a> c9 ..... I should have <h1> Category 3
您可以尝试寻找以前的h1。但是当缺少 1 h1 时,这会把它搞砸。 我不熟悉 VB.NET 中的 HtmlAgilityPack,所以我用 C# 编写了它。只是给你一个想法。
var headingNode = node.PreviousSibling;
while (headingNode != null && (headingNode.Name.Equals("h4") || headingNode.Name.Equals("#text")))
{
headingNode = headingNode.PreviousSibling;
}
//We will have first non h4 or #text here