0

次のノードがあります

"/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();
}
4

2 に答える 2

0

各ノードに「ツリー全体の深さ優先の位置」を割り当てることを検討してください。このようにして、2つのノードを比較するのは非常に簡単です。

ノードに任意のデータを添付できる場合は、直接追加します。それ以外の場合は、マップを配置するためのすべてのノードの辞書があります。

この比較を行う必要がある回数によっては、このアプローチは遅くなる可能性がありますが、実装と測定は簡単であり、要件を満たしていることに注意してください。

于 2012-03-08T18:10:42.260 に答える
0

このようにしましたか:

    protected string GuessThumbnail(HtmlDocument document)
    {
        HtmlNode root = document.DocumentNode;
        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 parent = description.ParentNode;
            while (parent != null)
            {
                string path = string.Concat(parent.XPath, ImageXPath);
                IList<HtmlNode> images = root.SelectNodesOrEmpty(path).ToList();

                // find the image tag that's closest to the text node.
                if (images.Any())
                {
                    HtmlNode descriptionOutermost = description.ParentNodeUntil(parent); // get the first child towards the description from the parent node.
                    int descriptionIndex = descriptionOutermost.GetIndex(); // get the index of the description's outermost element.

                    HtmlNode closestToDescription = null;
                    int distanceToDescription = int.MaxValue;

                    foreach (HtmlNode image in images)
                    {
                        int index = image.ParentNodeUntil(parent).GetIndex(); // get the index of the image's outermost element.
                        if (index > descriptionIndex)
                        {
                            index *= -1;
                        }
                        int distance = descriptionIndex - index;
                        if (distance < distanceToDescription)
                        {
                            closestToDescription = image;
                            distanceToDescription = distance;
                        }
                    }
                    if (closestToDescription != null)
                    {
                        string source = closestToDescription.Attributes["src"].Value;
                        return source;
                    }
                }

                parent = parent.ParentNode;
            }
        }
        // figure some other way to do it

        throw new NotImplementedException();
    }


public static HtmlNode ParentNodeUntil(this HtmlNode node, HtmlNode parent)
{
    while (node.ParentNode != parent)
    {
        node = node.ParentNode;
    }
    return node;
}
public static int GetIndex(this HtmlNode node)
{
    return node.ParentNode.ChildNodes.IndexOf(node);
}
于 2012-03-08T18:27:31.293 に答える