HTMLAgilityPack Selectnodes 总是 returns null
HTMLAgilityPack Selectnodes always returns null
我听说过有关 HTMLAgilityPack 库的好消息,所以我想我会试一试,但我的成功率绝对为零。几个月来我一直在努力解决这个问题。无论我做什么,我都无法让这段代码给我除 null 以外的任何东西。我试过按照这个例子 (http://www.c-sharpcorner.com/uploadfile/9b86d4/getting-started-with-html-agility-pack/),但我没有得到相同的结果,我无法解释原因。
我尝试加载文件,然后 运行 选择节点到 select 所有超链接,但它总是 return 是一个空列表。我试过 select 各种节点(div、p、a、所有内容和任何东西),它总是 return 是一个空列表。我试过使用 doc.Descendants,我试过在本地和网络上使用不同的源文件,但我所做的任何事情都不会 return 得到实际结果。
我一定是忽略了一些重要的东西,但我想不通它是什么。我可能会错过什么?
代码:
public string GetSource()
{
try
{
string result = "";
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
if (!System.IO.File.Exists("htmldoc.html"))
throw new Exception("Unable to load doc");
doc.LoadHtml("htmldoc.html"); // copied locally to bin folder, confirmed it found the file and loaded it
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//a"); // Always returns null, regardless of what I put in here
if (nodes != null)
{
foreach (HtmlNode item in nodes)
{
result += item.InnerText;
}
}
else
{
// Every. Single. Time.
throw new Exception("No matching nodes found in document");
}
return result;
}
catch (Exception ex)
{
return ex.ToString();
}
}
我正在使用的源 HTML 文件 'htmldoc.html' 如下所示:
<html>
<head>
<title>Testing HTML Agility Pack</title>
</head>
<body>
<div id="div1">
<a href="div1-a1">Link 1 inside div1</a>
<a href="div1-a2">Link 2 inside div1</a>
</div>
<a href="a3">Link 3 outside all divs</a>
<div id="div2">
<a href="div2-a1">Link 1 inside div2</a>
<a href="div2-a2">Link 2 inside div2</a>
</div>
</body>
</html>
要加载文件,您应该使用 Load
方法。LoadHtml
用于包含 html
的字符串
doc.Load("htmldoc.html");
我听说过有关 HTMLAgilityPack 库的好消息,所以我想我会试一试,但我的成功率绝对为零。几个月来我一直在努力解决这个问题。无论我做什么,我都无法让这段代码给我除 null 以外的任何东西。我试过按照这个例子 (http://www.c-sharpcorner.com/uploadfile/9b86d4/getting-started-with-html-agility-pack/),但我没有得到相同的结果,我无法解释原因。
我尝试加载文件,然后 运行 选择节点到 select 所有超链接,但它总是 return 是一个空列表。我试过 select 各种节点(div、p、a、所有内容和任何东西),它总是 return 是一个空列表。我试过使用 doc.Descendants,我试过在本地和网络上使用不同的源文件,但我所做的任何事情都不会 return 得到实际结果。
我一定是忽略了一些重要的东西,但我想不通它是什么。我可能会错过什么?
代码:
public string GetSource()
{
try
{
string result = "";
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
if (!System.IO.File.Exists("htmldoc.html"))
throw new Exception("Unable to load doc");
doc.LoadHtml("htmldoc.html"); // copied locally to bin folder, confirmed it found the file and loaded it
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//a"); // Always returns null, regardless of what I put in here
if (nodes != null)
{
foreach (HtmlNode item in nodes)
{
result += item.InnerText;
}
}
else
{
// Every. Single. Time.
throw new Exception("No matching nodes found in document");
}
return result;
}
catch (Exception ex)
{
return ex.ToString();
}
}
我正在使用的源 HTML 文件 'htmldoc.html' 如下所示:
<html>
<head>
<title>Testing HTML Agility Pack</title>
</head>
<body>
<div id="div1">
<a href="div1-a1">Link 1 inside div1</a>
<a href="div1-a2">Link 2 inside div1</a>
</div>
<a href="a3">Link 3 outside all divs</a>
<div id="div2">
<a href="div2-a1">Link 1 inside div2</a>
<a href="div2-a2">Link 2 inside div2</a>
</div>
</body>
</html>
要加载文件,您应该使用 Load
方法。LoadHtml
用于包含 html
doc.Load("htmldoc.html");