0

私は小さなウェブ分析ツールを作成しており、Xを超える量の単語を含む特定のURLのすべてのテキストブロックを何らかの方法で抽出する必要があります。

私が現在使用している方法は次のとおりです。

        public string getAllText(string _html)
    {
        string _allText = "";
        try
        {
            HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(_html);


            var root = document.DocumentNode;
            var sb = new StringBuilder();
            foreach (var node in root.DescendantNodesAndSelf())
            {
                if (!node.HasChildNodes)
                {
                    string text = node.InnerText;
                    if (!string.IsNullOrEmpty(text))
                        sb.AppendLine(text.Trim());
                }
            }

            _allText = sb.ToString();

        }
        catch (Exception)
        {
        }

        _allText = System.Web.HttpUtility.HtmlDecode(_allText);

        return _allText;
    }

ここでの問題は、多くのテキスト、3語のフッターテキストなどであっても、すべてのテキストが返されることです。

ページ上の実際のコンテンツを分析したいので、私の考えは、コンテンツである可能性のあるテキスト(つまり、X語を超えるテキストブロック)のみを解析することです。

これをどのように達成できるかについてのアイデアはありますか?

4

1 に答える 1

1

最初のアプローチは、 string.Split関数node.InnerTextを使用した各値の単純な単語数分析です。

string[] words;
words = text.Split((string[]) null, StringSplitOptions.RemoveEmptyEntries);

words.Lengthが 3 より大きいテキストのみを追加します。

また、未加工のテキストを収集するためのその他のトリックについては、この質問の回答を参照してください。

于 2012-11-17T08:56:07.197 に答える