Prithvi Raj Nandiwal
Prithvi Raj Nandiwal

Reputation: 3284

HtmlAgilityPack select Node return all Node from document

I want to scrap data from a website when select node from a node using class All Element selected from document

this is my code..

  var baseUri = new Uri("http://www.coupondunia.in/flipkart");
        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        WebClient client = new WebClient();
        doc.Load(client.OpenRead(baseUri));
        HtmlNodeCollection div = doc.DocumentNode.SelectNodes("//div[contains(@class,'detail-coupons') and contains(@class,'inner')] ");   
        foreach (HtmlNode item in div)
        {
            var Linksx = item.SelectSingleNode("//a[contains(@class,'emptyAnchorCouponPage couponTitle couponTitle_inplaceEdit clickableCoupon')]");
             var Links = item.SelectNodes("//a[contains(@class,'couponTitle') and contains(@class,'emptyAnchorCouponPage') and contains(@class,'couponTitle_inplaceEdit') and contains(@class,'clickableCoupon')]");          
        }

In Links and Linkx return multiple records

Upvotes: 0

Views: 438

Answers (1)

har07
har07

Reputation: 89285

To tell HtmlAgilityPack that your XPath scope is limited to current item you need to add a dot/period (.) at the beginning of the XPath :

foreach (HtmlNode item in div)
{
    var Linksx = item.SelectSingleNode(".//a[contains(@class,'emptyAnchorCouponPage couponTitle couponTitle_inplaceEdit clickableCoupon')]");
    var Links = item.SelectNodes(".//a[contains(@class,'couponTitle') and contains(@class,'emptyAnchorCouponPage') and contains(@class,'couponTitle_inplaceEdit') and contains(@class,'clickableCoupon')]");          
}

Otherwise the XPath scope considered is the entire HtmlDocument.

Upvotes: 2

Related Questions