代码之家  ›  专栏  ›  技术社区  ›  Churchill

阅读长字符串的前3段。[C,HTML AgilityPack]

  •  0
  • Churchill  · 技术社区  · 15 年前

    我想从一个长字符串中读取,并只输出字符串的前3段。我怎样才能做到这一点?我想用这段代码来显示单词的数量,但后来改成了段落。

    public string MySummary(string html, int max)
    {
        string summaryHtml = string.Empty;
    
        // load our html document
        HtmlDocument htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(html);
    
        int wordCount = 0;
    
    
    
    
        foreach (var element in htmlDoc.DocumentNode.ChildNodes)
        {
            // inner text will strip out all html, and give us plain text
            string elementText = element.InnerText;
    
            // we split by space to get all the words in this element
            string[] elementWords = elementText.Split(new char[] { ' ' });
    
            // and if we haven't used too many words ...
    
            if (wordCount <= max)
            {
                // add the *outer* HTML (which will have proper 
                // html formatting for this fragment) to the summary
                summaryHtml += element.OuterHtml;
                wordCount += elementWords.Count() + 1;
    
            }
            else
            {
                break;
            }
        }
    
        return summaryHtml ;
    }
    
    5 回复  |  直到 15 年前
        1
  •  2
  •   Andrew Bullock    15 年前

    如果你说的段落 <p> 标记,获取文档的所有子节点 <p> 把前3个的内部文本拉出来?

    编辑评论:

    RTFM?

    http://htmlagilitypack.codeplex.com/wikipage?title=Examples&referringTitle=Home

    类似于:

    string.Join(doc.DocumentElement.SelectNodes("//p").Take(3).Select(n => n.Text).ToArray(), " ");
    
        2
  •  0
  •   rlee923    15 年前

    为什么不直接使用字符串标记器并读到前面的where forth

    找到了吗?

        3
  •  0
  •   Derek Ekins    12 年前

    我只需要自己做这件事,并且想出了一个非常简单但又很宽容的方法来做这件事,对我们的特定场景来说效果很好:

        public string GetParagraphs(string html, int numberOfParagraphs)
        {
            const string paragraphSeparator = "</p>";
            var paragraphs = html.Split(new[] { paragraphSeparator }, StringSplitOptions.RemoveEmptyEntries);
            return string.Join("", paragraphs.Take(numberOfParagraphs).Select(paragraph => paragraph + paragraphSeparator));
        }
    

    我知道这对于文档的结构是多么幼稚,它也会得到任何 <p> 标记之间 <p> ,但是在我的用例中,这正是我想要的——也许这对您也有用?

        4
  •  0
  •   Pino    10 年前

    这是更好的答案。但是如果我们想把第2段改为第5段,那么代码是什么呢?

    public string GetParagraphs(string html, int numberOfParagraphs) {
        const string paragraphSeparator = "</p>";
        var paragraphs = html.Split(new[] { paragraphSeparator }, StringSplitOptions.RemoveEmptyEntries);
        return string.Join("", paragraphs.Take(numberOfParagraphs).Select(paragraph => paragraph + paragraphSeparator));
    }
    
        5
  •  0
  •   StefanM MarkOwen320    8 年前

    你必须使用htmlagilitypack。

    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml(HtmlContent);
    
    string Html = string.Join(" ", doc.DocumentNode.SelectNodes("//p").Take(2).Select(n => n.OuterHtml).ToArray());
    
    string Html = string.Join(" ", doc.DocumentNode.SelectNodes("//p").Take(2).Select(n => n.OuterHtml).ToArray());