gpt4 book ai didi

c# - 如何使用 HtmlAgility Pack 解析 WPF 中 div id 标记之间的值?

转载 作者:行者123 更新时间:2023-11-28 09:17:07 26 4
gpt4 key购买 nike

我有一个这样的 html 文件:

<div id="id_1">
Some Texts
</div>
<div id ="id_2">
Some Texts
</div>

如何获取每个 div id 标签之间的所有文本?我的问题与 WPF 有关。

这是我的代码:

private void button_click(object sender, RoutedEventArgs e) {
HtmlDocument doc = new HtmlDocument();
doc.Load("file.html");
HtmlNode nodes = doc.DocumentNode.SelectNodes("//div[@id='id_1']");
var text = nodes.InnerText;
MessageBox.Show(text);
}

最佳答案

我正在抓取这个来源:

<div id="id_1">
Some Texts 1
</div>
<div id ="id_2">
Some Texts 2
</div>

使用:

HtmlDocument doc = new HtmlDocument();
doc.Load("C:\\temp\\stackhtml.html");
int i = 1;
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div");
foreach(HtmlNode node in nodes)
{
string text = node.InnerText;
Console.WriteLine("text" + i.ToString() + ": " + text);
i++;
}
Console.ReadLine();

结果:

enter image description here

你可以随心所欲地处理字符串、填充和数组等......

关于c# - 如何使用 HtmlAgility Pack 解析 WPF 中 div id 标记之间的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26263478/

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