-1

I am trying to scrape a website using HTML Agility Pack, but have run into an error. The node variable always throws a NullReferenceException when debugging. Why?

 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();
    }
kaptcha
  • 68
  • 2
  • 10

1 Answers1

1

It looks like no results were found when you are querying using that XPath expression, a null response is a completely valid response when calling DocumentNode.SelectNodes

An HtmlAgilityPack.HtmlNodeCollection containing a collection of nodes matching the HtmlAgilityPack.HtmlNode.XPath query, or null if no node matched the XPath expression.

When writing Xpath you might find it useful to use a plugin to test your xpath on the browser. I've used this Xpath Finder plugin before

Joshua Duxbury
  • 4,892
  • 4
  • 32
  • 51