C# 为什么`node`返回null?

C# 为什么`node`返回null?,c#,html-agility-pack,C#,Html Agility Pack,我正试图使用HTML敏捷包抓取一个网站,但遇到了一个错误。调试时,节点变量始终抛出NullReferenceException。为什么? static void Main ( string [] args ) { var html = @"https://www.amazon.com/s?k="; Console.WriteLine("Enter a product: "); string item = Console.ReadLine

我正试图使用HTML敏捷包抓取一个网站,但遇到了一个错误。调试时,
节点
变量始终抛出
NullReferenceException
。为什么?

 static void Main ( string [] args )
    {
        var html = @"https://www.amazon.com/s?k=";
        Console.WriteLine("Enter a product:  ");
        string item = Console.ReadLine();
        string closing = @"&ref=nb_sb_noss_2";

        item = item.Replace(' ', '+');

        var uri = new Uri(html + item + closing);

        HtmlWeb web = new HtmlWeb();

        HtmlDocument htmlDoc = web.Load(uri);
        var node = htmlDoc.DocumentNode.SelectNodes($"//div[@class='sg-col-inner']");
        double price = 0.0;
        string spWhole, spDecimal, name, bestName = "";
        int myI = 0;

        foreach (HtmlNode product in node)
        {
            Console.Write(myI + "... ");
            name = product.SelectSingleNode("//span[@class='a-size-medium a-color-base a-text-normal']").InnerText;
            spWhole = product.SelectSingleNode("//span@[class='a-price-whole']").InnerText;
            spDecimal = product.SelectSingleNode("//span[@class='a-price-fraction']").InnerText;
            double nPrice = Convert.ToDouble(spWhole);
            nPrice += Convert.ToDouble(spDecimal)/100;

            if (nPrice > price)
            {
                price = nPrice;
                bestName = name;
            }

            using (StreamWriter sw = new StreamWriter("Prices.txt"))
            {
                sw.WriteLine(name + ":  " + nPrice);
            }
            myI++;
        }

        Console.WriteLine(bestName + ":  " + price);


        Console.Read();
    }

在使用该XPath表达式进行查询时,似乎找不到任何结果,在调用DocumentNode.SelectNodes时,空响应是完全有效的响应

包含节点集合的HtmlAlityPack.HtmlNodeCollection 匹配HtmlAgilityPack.HtmlNode.XPath查询,如果没有节点,则为null 匹配XPath表达式


在编写Xpath时,您可能会发现使用插件在浏览器上测试Xpath很有用。我以前使用过这个插件

很明显,您的xPath无效,找不到任何节点。但是,我对您的DOM没有任何线索,因此正确的路径是什么。@HimBromBreere
$“//div[@class='sg-col-inner']”
似乎不正确。不,我不知道上面提到的正确答案。也许没有那个级别的div,没有。它没有回答这个问题,这是因为脚本不是由HAP执行的。如果你使用的是chrome,它会呈现页面并执行脚本,你会看到与HAP提供的不同的html