C# Html敏捷包中的SelectNode返回Null
我看到很多类似的帖子,它们都在谈论SelectSingleNodeReturnNull。我不太确定我的问题是否与此有关。也许我有一些我想不出来的问题。这是我的密码:C# Html敏捷包中的SelectNode返回Null,c#,html,html-agility-pack,C#,Html,Html Agility Pack,我看到很多类似的帖子,它们都在谈论SelectSingleNodeReturnNull。我不太确定我的问题是否与此有关。也许我有一些我想不出来的问题。这是我的密码: string url = "https://www.google.com/#q=nothing"; HtmlWeb web = new HtmlWeb(); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); web.Load(url)
string url = "https://www.google.com/#q=nothing";
HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
web.Load(url);
var nodes = doc.DocumentNode.SelectNodes("//div[@class='content']");
if (nodes != null) {
foreach(HtmlNode item in nodes) {
if (item != null) {
string s = item.InnerText;
listView1.Items.Add(s);
}
}
} else {
MessageBox.Show("Nothing found here");
}
如果没有class等于content
的
标记,则找不到任何内容,您的为null
。那是故意的
更新:您没有将数据加载到HtmlDocument
。您有与正在加载的数据无关的doc
实例。使用Load
方法返回的文档:
HtmlDocument doc = web.Load(url);