【问题标题】:get p tags that come after a specific tag using htmlagilitypack使用 htmlagilitypack 获取特定标签之后的 p 个标签
【发布时间】:2011-11-01 20:16:05
【问题描述】:
我正在使用 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>
我想把所有的 Ps 都拿到之后
有没有办法使用位置来过滤 Ps。
哪里(位置(p)>位置(h3))
【问题讨论】:
标签:
c#
web-crawler
html-agility-pack
【解决方案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");
【解决方案2】:
试试下面的代码:
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);