gpt4 book ai didi

c# - 如何在 C# 中使用 HtmlAgilityPack 获取 HTML 元素的内容?

转载 作者:太空狗 更新时间:2023-10-30 01:10:36 25 4
gpt4 key购买 nike

我想在 C# 中使用 HTMLAgilityPack 从 HTML 页面获取有序列表的内容,我已经尝试了以下代码但是,这不起作用任何人都可以帮助,我想传递 html 文本并获取内容在 html 中找到的第一个有序列表

private bool isOrderedList(HtmlNode node)
{
if (node.NodeType == HtmlNodeType.Element)
{
if (node.Name.ToLower() == "ol")
return true;
else
return false;
}
else
return false;
}

public string GetOlList(string htmlText)
{
string s="";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlText);
HtmlNode nd = doc.DocumentNode;
foreach (HtmlNode node in nd.ChildNodes)
{
if (isOrderedList(node))
{
s = node.WriteContentTo();
break;
}
else if (node.HasChildNodes)
{
string sx= GetOlList(node.WriteTo());
if (sx != "")
{
s = sx;
break;
}
}
}
return s;
}

最佳答案

下面的代码对我有用

public static string GetComments(string html)
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
string s = "";
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//ol"))
{
s += node.OuterHtml;
}

return s;
}

关于c# - 如何在 C# 中使用 HtmlAgilityPack 获取 HTML 元素的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4358696/

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