read product on website

Topics: User Forum
Jul 17, 2009 at 6:18 PM

I am trying to find a product on this website

view-source:http://www.babydirect.com/p-13285-angel-line-round-crib-cherry-free-shipping.aspx

i see that it have a class id has <h2 class="HeaderBlue">

how would i do this using the html agility. 

 

            HtmlAgilityPack.HtmlWeb htmlWeb = new HtmlAgilityPack.HtmlWeb();
            HtmlAgilityPack.HtmlDocument doc = htmlWeb.Load(sPath);
            HtmlAgilityPack.HtmlNodeCollection links = doc.DocumentNode.SelectNodes("<h2HeaderBlue" + ">");

            HtmlAgilityPack.HtmlWeb htmlWeb = new HtmlAgilityPack.HtmlWeb();

            HtmlAgilityPack.HtmlDocument doc = htmlWeb.Load(sPath);

 

            HtmlAgilityPack.HtmlNodeCollection links = doc.DocumentNode.SelectNodes("<h2 class=" + "HeaderBlue" + ">");

 

Aug 2, 2009 at 12:15 PM
Edited Aug 2, 2009 at 12:17 PM

In this instance, the webpage only has 1 H2 with class HeaderBlue. But when they change the colors to red, it might become HeaderRed, so you know.

HtmlWeb web = new HtmlWeb();
HtmlDocument doc =web.Load(url);

HtmlNode h2blue = doc.SelectSingleNode("//h2[@class='HeaderBlue']");
if( h2blue != null )
{
    // Do stuff.
}

// Or

HtmlNodeCollection h2s = doc.SelectNodes("//h2[@class='HeaderBlue']");
if( h2s != null && h2s.Count > 0 )
{
   foreach( HtmlNode node in h2s )
   {
       // Do stuff.
   }
}



Hope this helps.

What's used to actually traverse the path to wanted nodes is called XPath. It's used as a stylesheet for XML files, they can transform XML files into most things. You can read a short tutorial about it on W3Schools.com