我正在使用htmlagilitypack c#:
抓取网站i have in the source code of an html page
....
<p>this a p that come before h3</p>
....
....
<h3>this h3 </h3>
<p>first p after h3</p>
....
<p>seconde p after h3</p>
我希望所有人都获得
之后的所有P ..有没有办法使用位置来过滤Ps。
其中(position(p)&gt; position(h3))
答案 0 :(得分:1)
您可以使用辅助方法,例如:
static IEnumerable<HtmlNode> FilteredTakeWhile(
HtmlNode root,
Func<HtmlNode, bool> predicate,
Func<HtmlNode, bool> takePredicate)
{
for (var currentNode = root.NextSibling;
currentNode != null && takePredicate(currentNode);
currentNode = currentNode.NextSibling)
{
if (predicate(currentNode))
yield return currentNode;
}
}
然后使用它:
var h3 = doc.DocumentNode.SelectSingleNode("h3");
// take all "p" nodes while we haven't reached the next "h3" node
var query = FilteredTakeWhile(h3, node => node.Name == "p", node => node.Name != "h3");
答案 1 :(得分:1)
请尝试以下代码:
var htmlText = "source code of your html page";
var htmlDoc.LoadHtml(htmlText);
var h3= htmlDoc.DocumentNode.SelectNodes("//h2");
var lineNum = h3[0].Line;
var p = htmlDoc.DocumentNode.SelectNodes("//p").Where(x => x.Line > lineNum);