C# 希望使用 HtmlAgilityPack 获取 <div> 值但收到 System.NullReferenceException
C# Looking to get obtain the <div> value using HtmlAgilityPack but receiving a System.NullReferenceException
我正在尝试获取 div class "darkgreen" 的值,即 46.98。我尝试了以下代码,但出现 Null 异常。
下面是我正在尝试的代码:
private void button1_Click(object sender, EventArgs e)
{
var doc = new HtmlWeb().Load("https://rotogrinders.com/grids/nba-defense-vs-position-cheat-sheet-1493632?site=fanduele");
HtmlAgilityPack.HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@class='darkgreen']");
foreach (HtmlAgilityPack.HtmlNode node in nodes)
{
Console.WriteLine(node.InnerText);
}
}
如果我 运行 相同的代码但使用 doc.DocumentNode.SelectNodes("//div[@class='rgt-hdr colorize']") 它确实拉header 数据没有错误。
我在想 child 个节点可能是一个解决方案,但我不确定,因为我无法让它继续工作。
您的问题是 HTML 您看起来是由 javascript 创建的。而您加载到文档变量中的 HTML 是由 javascript 创建的 pre-what-ever。如果您在 Web 浏览器中查看页面源代码,您将看到加载到 HtmlDocument 变量中的确切 HTML。
下面的示例将为您提供用于创建 table 的数据 (JSON)。我不知道这是否足以满足您尝试做的任何事情。
public static void Main(string[] args)
{
Console.WriteLine("Program Started!");
HtmlDocument doc;
doc = new HtmlWeb().Load("https://rotogrinders.com/grids/nba-defense-vs-position-cheat-sheet-1493632?site=fanduele");
HtmlNode node = doc.DocumentNode.SelectSingleNode("//section[@class='bdy content article full cflex reset long table-page']/following-sibling::script[1]");
int start = node.InnerText.IndexOf("[");
int length = node.InnerText.IndexOf("]") - start +1;
Console.WriteLine(node.InnerText.Substring(start, length));
Console.WriteLine("Program Ended!");
Console.ReadKey();
}
备选方案
或者,您可以将 Selenium 与 PhantomJS 一起使用。然后将 HTML 从无头浏览器加载到您的文档变量中,然后您的 xpath 将起作用。
我正在尝试获取 div class "darkgreen" 的值,即 46.98。我尝试了以下代码,但出现 Null 异常。
下面是我正在尝试的代码:
private void button1_Click(object sender, EventArgs e)
{
var doc = new HtmlWeb().Load("https://rotogrinders.com/grids/nba-defense-vs-position-cheat-sheet-1493632?site=fanduele");
HtmlAgilityPack.HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@class='darkgreen']");
foreach (HtmlAgilityPack.HtmlNode node in nodes)
{
Console.WriteLine(node.InnerText);
}
}
如果我 运行 相同的代码但使用 doc.DocumentNode.SelectNodes("//div[@class='rgt-hdr colorize']") 它确实拉header 数据没有错误。
我在想 child 个节点可能是一个解决方案,但我不确定,因为我无法让它继续工作。
您的问题是 HTML 您看起来是由 javascript 创建的。而您加载到文档变量中的 HTML 是由 javascript 创建的 pre-what-ever。如果您在 Web 浏览器中查看页面源代码,您将看到加载到 HtmlDocument 变量中的确切 HTML。
下面的示例将为您提供用于创建 table 的数据 (JSON)。我不知道这是否足以满足您尝试做的任何事情。
public static void Main(string[] args)
{
Console.WriteLine("Program Started!");
HtmlDocument doc;
doc = new HtmlWeb().Load("https://rotogrinders.com/grids/nba-defense-vs-position-cheat-sheet-1493632?site=fanduele");
HtmlNode node = doc.DocumentNode.SelectSingleNode("//section[@class='bdy content article full cflex reset long table-page']/following-sibling::script[1]");
int start = node.InnerText.IndexOf("[");
int length = node.InnerText.IndexOf("]") - start +1;
Console.WriteLine(node.InnerText.Substring(start, length));
Console.WriteLine("Program Ended!");
Console.ReadKey();
}
备选方案
或者,您可以将 Selenium 与 PhantomJS 一起使用。然后将 HTML 从无头浏览器加载到您的文档变量中,然后您的 xpath 将起作用。