C# 使用HtmlAgilityPack通过innerHTML进行解析
只是想弄清楚如何从已经解析的信息中解析信息C# 使用HtmlAgilityPack通过innerHTML进行解析,c#,xpath,html-agility-pack,selectsinglenode,C#,Xpath,Html Agility Pack,Selectsinglenode,只是想弄清楚如何从已经解析的信息中解析信息 foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//div [@class=\"result-link\"]")) { if (node == null) Console.WriteLine("debug"); else { //string h_url = node.Attributes["a"].Value; Co
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//div [@class=\"result-link\"]"))
{
if (node == null)
Console.WriteLine("debug");
else
{
//string h_url = node.Attributes["a"].Value;
Console.WriteLine(node.InnerHtml);
}
}
所以你可以看到我试图用“string h_url”声明做什么。在“result link”div类中有一个a href属性,我试图获取href值。所以基本上是联系
我好像想不出来。我已尝试使用属性数组:
string h_url = node.Attributes["//a[@href].Value;
没有运气 可以使用XPath选择与当前节点相关的元素:
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@class='result-link']");
if (nodes != null)
{
foreach (HtmlNode node in nodes)
{
HtmlNode a = node.SelectSingleNode("a[@href]");
if (a != null)
{
// use a.Attributes["href"];
}
// etc...
}
}