HtmlAgilityPack:如何解释 HTML 中的非标记文本
知道标题有点模糊,这里有一个例子,
<DIV>
<DIV>title1</DIV>
line1<br/>
line2<br/>
<DIV>title2</DIV>
line2.1<br/>
line2.2<br/>
</DIV>
How can I fetch line1
for title1, and
gt;line2
gt;line2.1
? 我同时使用 HtmlAgilityPack 和 SharpQuery。
line2.2
谢谢。
可能的解决方案
经过更多研究和尝试,我设法使用 LinePosition 和 "//div/text()" 获取这些内容
public static HtmlNodeCollection getNodes(string html, string xpath)
{
if (html.Length <= 0) { return null; }
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
return doc.DocumentNode.SelectNodes(xpath);
}
foreach (HtmlNode node in getNodes(html, "//div"){
foreach (HtmlNode plain_node in getNodes(html, "//div/text()")
{
if (plain_node.LinePosition <= node.LinePosition)
{
currentHtml += plain_n.InnerHtml + "<br/>";
}
}
}
还有其他更好的方法吗?
Know that the title is kind of vague, here is an example,
<DIV>
<DIV>title1</DIV>
line1<br/>
line2<br/>
<DIV>title2</DIV>
line2.1<br/>
line2.2<br/>
</DIV>
How can I fetch line1<br/>line2<br/>
for title1, and line2.1<br/>line2.2<br/>
for title2?
I'm using HtmlAgilityPack and SharpQuery together.
Thanks.
Possible Resolution
After researched and tried more, I managed to fetch these by using LinePosition and "//div/text()"
public static HtmlNodeCollection getNodes(string html, string xpath)
{
if (html.Length <= 0) { return null; }
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
return doc.DocumentNode.SelectNodes(xpath);
}
foreach (HtmlNode node in getNodes(html, "//div"){
foreach (HtmlNode plain_node in getNodes(html, "//div/text()")
{
if (plain_node.LinePosition <= node.LinePosition)
{
currentHtml += plain_n.InnerHtml + "<br/>";
}
}
}
Any other better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
html 匹配问题很少有一种独特的解决方案。尽管您的解决方案现在可以很好地使用您的示例,但
//div
表达式将递归地搜索 root 下的所有div
元素。这意味着如果原始的 Html 以某种方式演变,您可能会捕获太多的东西或分析太多的节点(对于大文档来说,性能可能是 // 等问题)。
我建议像这样的东西,这更具区分性:
这意味着
div
元素div
元素text
text
类型的所有后续同级元素请参阅此链接以获取有关 XPATH 轴。
There is rarely one unique solution to an html matching problem. Although your solution works fine now and with your sample, the
//div
expression will search alldiv
elements under root, recursively.It means if the original Html evolves somehow, you may catch too many things or analyze too many nodes (performance may be an issue with things like // for big documents).
I would suggest something like this, which is more discriminant:
It means
div
elements from the rootdiv
elements underneathtext
that start with 'title'text
See this link for some help on XPATH Axes.
假设结构始终相同,您可以获取 div,然后获取它们的 NextSiblings
Assuming the structure is always the same you could get the divs and then get both of their NextSiblings