HTML AGILITY PACK Parsing Div Blocks

.net c# html html-agility-pack

Question

I need the name and price of the things I need to parse from an online store. Every item-block is situated somewhere distinct.div inside adiv-catalog of those things.
I gave it a go, and although it sort of works, I would love to parse both the name and the price in a single loop. How can I proceed? Thanks!

        var url = "http://bestaqua.com.ua/catalog/filtry-obratnogo-osmosa";
        HtmlWeb web = new HtmlWeb();
        HtmlDocument HtmlDoc = web.Load(url);
        var RootNode = HtmlDoc.DocumentNode;
        foreach (HtmlNode node in 
HtmlDoc.DocumentNode.SelectNodes("//div[@class='catalog_blocks']"))
        {
            foreach (HtmlNode item_name in 
node.SelectNodes("//div[@class='catalog_blocks-item-name']"))
            {
                    string name = item_name.InnerText;
                    System.Diagnostics.Debug.Write("NAME :" + name + "\n" );
            }
            foreach (HtmlNode item_price in 
node.SelectNodes("//span[@class='price-new']"))
            {
                string price = item_price.InnerText;
                System.Diagnostics.Debug.Write("PRICE: " + price + "\n");
            }
        }
1
0
2/14/2018 9:46:45 PM

Accepted Answer

Since SelectNodes If utilizing a XPATH-expression, you can just use a union in your class filter using the operator "|," which will produce a single collection to loop through. Keep in mind that you would then need to verify the part you had really chosen within the for-loop.

1
2/14/2018 9:55:14 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