Vam*_*msi 5 html c# html-parsing html-agility-pack
我想使用 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;
}
Run Code Online (Sandbox Code Playgroud)
以下代码对我有用
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;
}
Run Code Online (Sandbox Code Playgroud)