无法使用 HTMLAgilityPack 找到节点
Can't find node using HTMLAgilityPack
我使用了以下视频中的代码示例:https://youtu.be/8e3Wklc1H_A
代码如下所示
var webGet = new HtmlWeb();
var doc = webGet.Load("http://pastebin.com/raw.php?i=gF0DG08s");
HtmlNode OurNone = doc.DocumentNode.SelectSingleNode("//div[@id='footertext']");
if (OurNone != null)
richTextBox1.Text = OurNone.InnerHtml;
else
richTextBox1.Text = "nothing found";
一开始我以为原网站可能已经挂了(www.fuchsonline.com)所以我很快做了一个只有页脚的HTML并粘贴到Pastebin上(link 在上面的代码中)
<html>
<body>
<div id="footertext">
<p>
Copyright © FUCHS Online Ltd, 2013. All Rights Reserved.
</p>
</div>
</body>
</html>
在代码中使用 Pastebin link 时,程序总是将 "nothing found" 写入 richTextBox。但是,视频中使用的网站仍然可用,所以我尝试在 webGet 中使用该网站,瞧 - 它有效。
现在我想问一下每个代码到底有什么问题。 HTML 是否遗漏了什么,或者该程序是否仅适用于完整的网站?如果是,是什么让网站完整?
在这种情况下,您只是将原始 html 作为字符串保存到此页面,这就是它返回空的原因。如果你真的想用 HTML 敏捷包解析它,你可以先下载页面,获取原始 HTML,然后将其解析到敏捷包的文档模型中。
WebRequest webRequest = HttpWebRequest.Create("http://pastebin.com/raw.php?i=gF0DG08s");
webRequest.Method = "GET";
string pageSource;
using (StreamReader reader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
{
pageSource = reader.ReadToEnd();
HtmlDocument html = new HtmlDocument();
html.LoadHtml(pageSource);
HtmlNode OurNone = html.DocumentNode.SelectSingleNode("//div[@id='footertext']");
if (OurNone != null)
{
richTextBox1.Text = OurNone.InnerHtml;
}
else
{
richTextBox1.Text = "nothing found";
}
}
这里有一个更简单的方法:
WebClient webClient = new WebClient();
string htmlCode = webClient.DownloadString("http://pastebin.com/raw.php?i=gF0DG08s");
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(htmlCode);
HtmlNode OurNone = doc.DocumentNode.SelectSingleNode("//div[@id='footertext']");
if (OurNone != null)
richTextBox1.Text = OurNone.InnerHtml;
else
richTextBox1.Text = "nothing found";
我使用了以下视频中的代码示例:https://youtu.be/8e3Wklc1H_A
代码如下所示
var webGet = new HtmlWeb();
var doc = webGet.Load("http://pastebin.com/raw.php?i=gF0DG08s");
HtmlNode OurNone = doc.DocumentNode.SelectSingleNode("//div[@id='footertext']");
if (OurNone != null)
richTextBox1.Text = OurNone.InnerHtml;
else
richTextBox1.Text = "nothing found";
一开始我以为原网站可能已经挂了(www.fuchsonline.com)所以我很快做了一个只有页脚的HTML并粘贴到Pastebin上(link 在上面的代码中)
<html>
<body>
<div id="footertext">
<p>
Copyright © FUCHS Online Ltd, 2013. All Rights Reserved.
</p>
</div>
</body>
</html>
在代码中使用 Pastebin link 时,程序总是将 "nothing found" 写入 richTextBox。但是,视频中使用的网站仍然可用,所以我尝试在 webGet 中使用该网站,瞧 - 它有效。
现在我想问一下每个代码到底有什么问题。 HTML 是否遗漏了什么,或者该程序是否仅适用于完整的网站?如果是,是什么让网站完整?
在这种情况下,您只是将原始 html 作为字符串保存到此页面,这就是它返回空的原因。如果你真的想用 HTML 敏捷包解析它,你可以先下载页面,获取原始 HTML,然后将其解析到敏捷包的文档模型中。
WebRequest webRequest = HttpWebRequest.Create("http://pastebin.com/raw.php?i=gF0DG08s");
webRequest.Method = "GET";
string pageSource;
using (StreamReader reader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
{
pageSource = reader.ReadToEnd();
HtmlDocument html = new HtmlDocument();
html.LoadHtml(pageSource);
HtmlNode OurNone = html.DocumentNode.SelectSingleNode("//div[@id='footertext']");
if (OurNone != null)
{
richTextBox1.Text = OurNone.InnerHtml;
}
else
{
richTextBox1.Text = "nothing found";
}
}
这里有一个更简单的方法:
WebClient webClient = new WebClient();
string htmlCode = webClient.DownloadString("http://pastebin.com/raw.php?i=gF0DG08s");
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(htmlCode);
HtmlNode OurNone = doc.DocumentNode.SelectSingleNode("//div[@id='footertext']");
if (OurNone != null)
richTextBox1.Text = OurNone.InnerHtml;
else
richTextBox1.Text = "nothing found";