我想使用从文档
中选择的类所有元素从节点中选择节点时从网站中删除数据这是我的代码..
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')]");
}
在Links
和Linkx
中返回多条记录
答案 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
。