HtmlAgilityPack选择节点从文档返回所有节点

时间:2014-06-23 16:42:50

标签: asp.net web-scraping html-agility-pack

我想使用从文档

中选择的类所有元素从节点中选择节点时从网站中删除数据

这是我的代码..

  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')]");          
        }

LinksLinkx中返回多条记录

1 个答案:

答案 0 :(得分:2)

告诉HtmlAgilityPack您的XPath范围仅限于当前item,您需要在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')]");          
}

否则考虑的XPath范围是整个HtmlDocument