How to choose the right span class in HTML Agility Pack

c# html-agility-pack

Question

I'm looking for the best deal on Amazon pages. Use this link as an illustration:

http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=9963BB#/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=E999-4701&rh=i%3Aaps%2Ck%3AE999-4701

The number to the right of "new from" is where I want to get the lowest price.

Here are the things I've tried:

        using (TextWriter tw = new StreamWriter(@"D:\AmazonUrls.txt"))
        {
            foreach (string item in list)
            {
                var webGet = new HtmlWeb();
                var document = webGet.Load(item);
                var lowestPrice = document.DocumentNode.SelectSingleNode("//span[@id='subPrice']");
                if (lowestPrice != null)
                {
                    Console.WriteLine(lowestPrice);                
                }

            }           
        }

I'm not seeing any progress. Where am I making a mistake?

1
3
4/10/2012 8:43:34 PM

Accepted Answer

You're requesting nodes with anid a subPrice, but it's reallyclass which subPrice:

<span class="subPrice">
        <a href="http://www.amazon.com/gp/offer-listing/B001BA0W06/ref=sr_1_6_olp?ie=UTF8&qid=1334090832&sr=8-6&condition=new">5 new</a>
    from <span class="price">$245.90</span></span>

so,

var lowestPrice = document.DocumentNode.SelectSingleNode("//span[@class='subPrice']");

you ought to achieve your goals. The difficulty is that you want to choose numerous nodes and then loop over them to see which has the lowest privce since the sample page you provide has several nodes that meet that pattern.

5
4/10/2012 8:49:18 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow