2016-04-11 6 views
0

残るHtmlの敏捷性パックのテキスト:</form>タグは、私はHTMLの敏捷性パック付きHTMLページからテキストだけを取得する2つの方法を試してみた

方法1

var root = doc.DocumentNode; 

foreach (HtmlNode node in root.SelectNodes("//text()")) 
{ 
    sb.AppendLine(node.InnerText.Trim() + " "); 
} 

に方法2

var root = doc.DocumentNode; 
foreach (var node in root.DescendantsAndSelf()) 
{ 
    if (!node.HasChildNodes) 
    { 
     string text = node.InnerText; 
     if (!string.IsNullOrEmpty(text)) 
      sb.AppendLine(text.Trim() + " "); 
    } 
} 

これらの両方が、prの場合は</form>タグの後ろに残りますページのesent。たとえば、www.google.com:

"body": " Search Images Maps Play YouTube News Gmail Drive More Calendar 
Translate Mobile Books Wallet Shopping Blogger Finance Photos Videos Docs 
Even more &raquo; Account Options Sign in Search settings Web History 
&times; Try a fast, secure browser with updates built in. Yes, get Chrome 
now &nbsp; Advanced search Language tools </form> Advertising Programs 
Business Solutions +Google About Google &copy; 2016 - Privacy - Terms " 

何が得られますか?

編集: "テキストだけ" 私は意味、 "言語テキスト" と....そう:

<i>book reports</i>book reports

<a href="...">More Details</a>なりCheck out our deals!

+0

あなたは「単なるテキスト」で、正確に何を意味するかを使用したい場合から引き出さ?結局のところ、htmlは単なるテキストです。各DOM要素の内容だけを意味しますか?または、他の何か? – dbugger

+0

多分、「言語テキスト」はそれを説明する良い方法でしょう。最終的には、これがサイト検索のためのデータベースに入ります。主な質問にいくつかの例を追加します。 – FirstDivision

答えて

0

してくださいになりMore Details

<div>Check out our <b>deals</b>!</div>なり投稿前に質問を検索

個の

Using C# regular expressions to remove HTML tags

サンプルは、このWebページ

String result = Regex.Replace(htmlDocument, @"<[^>]*>", String.Empty); 

それとも、アジリティ(また、Webページからプル)

HtmlDocument doc = new HtmlDocument(); 
doc.LoadHtml(Properties.Resources.HtmlContents); 
var text = doc.DocumentNode.SelectNodes("//body//text()").Select(node => node.InnerText); 
StringBuilder output = new StringBuilder(); 
foreach (string line in text) 
{ 
    output.AppendLine(line); 
} 
string textOnly = HttpUtility.HtmlDecode(output.ToString()); 
関連する問題