我有以下节点
"/html[1]/body[1]/div[1]/div[1]/div[3]/div[1]/div[7]/p[1]/#text[1]"
我怎样才能确定其中最后一个是最接近的?
"/html[1]/body[1]/div[1]/div[1]/div[3]/div[1]/div[4]/div[1]/img[1]"
"/html[1]/body[1]/div[1]/div[1]/div[3]/div[1]/div[4]/div[3]/a[1]/img[1]"
"/html[1]/body[1]/div[1]/div[1]/div[3]/div[1]/div[4]/div[3]/a[2]/img[1]"
"/html[1]/body[1]/div[1]/div[1]/div[3]/div[1]/div[4]/div[5]/img[1]"
"/html[1]/body[1]/div[1]/div[1]/div[3]/div[1]/div[5]/div[1]/img[1]"
它不一定总是最后一个。
这是我到达那里的方式:
protected string GuessThumbnail(HtmlDocument document)
{
HtmlNode root = document.DocumentNode;
IEnumerable<string> result = new List<string>();
HtmlNode description = root.SelectSingleNode(DescriptionPredictiveXPath);
if (description != null) // in this case, we predict relevant images are the ones closest to the description text node.
{
HtmlNode node = description.ParentNode;
while (node != null)
{
string path = string.Concat(node.XPath, ImageXPath);
node = node.ParentNode;
IEnumerable<HtmlNode> nodes = root.SelectNodesOrEmpty(path);
// find the image tag that's closest to the text node.
if (nodes.Any())
{
var xpaths = nodes.Select(n => n.XPath);
xpaths.ToList();
// return closest
}
}
}
// figure some other way to do it
throw new NotImplementedException();
}