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();
}