gpt4 book ai didi

c# - Html Agility Pack 从 div 获取特定内容

转载 作者:太空宇宙 更新时间:2023-11-04 13:06:59 24 4
gpt4 key购买 nike

我正在尝试从“div”中提取文本并排除其他所有内容。你能帮帮我吗?!

<div class="article">
<div class="date">01.01.2000</div>
<div class="news-type"><a href="../link/page01">Breaking News</a></div>

"Here is the location of the text i would like to pull"

</div>

当我提取“文章”类时,我得到了所有内容,但我无法/不知道如何排除 class="date"、class="news-type"以及其中的所有内容。

这是我使用的代码:

HtmlDocument doc = web.Load(url);
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//div[contains(@class,'article')]"))
{
name_text.text += node.InnerHtml.Trim();
}

谢谢!

最佳答案

另一种方法是使用 XPath /text()[normalize-space()] 中获取非空、直接子文本节点div 元素:

var divs = doc.DocumentNode.SelectNodes("//div[contains(@class,'article')]");
foreach (HtmlNode div in divs)
{
var node = div.SelectSingleNode("text()[normalize-space()]");
Console.WriteLine(node.InnerText.Trim());
}

dotnetfiddle demo

输出:

"Here is the location of the text i would like to pull"

关于c# - Html Agility Pack 从 div 获取特定内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39683019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com