开发者

Extracting Inner text from HTML BODY node with Html Agility Pack

开发者 https://www.devze.com 2023-03-23 22:16 出处:网络
Need a bit of help with HTML Agility Pack! Basically I want to grab plain-text withing the body node of the HTML.

Need a bit of help with HTML Agility Pack!

Basically I want to grab plain-text withing the body node of the HTML. So far I have tried this in vb.net and it fails to return the innertext meaning no change is seen, well atleast from what I can see.

Dim htmldoc As HtmlDocument = New HtmlDocument
htmldoc.LoadHtml(html)

Dim paragraph As HtmlNodeCollection = htmldoc.DocumentNode.SelectNodes("//body")

If Not htmldoc Is Nothing Then
   For Each node In paragraph
       node.ParentNode.RemoveChild(node, True)
   Next
End If

Return htmldoc.DocumentNode.WriteContentTo

I have tried this:

Return htmldoc.DocumentNode.InnerT开发者_如何学Pythonext

But still no luck!

Any advice???


How about:

Return htmldoc.DocumentNode.SelectSingleNode("//body").InnerText


Jeff's solution is ok if you haven't tables, because text located in the table is sticking like cell1cell2cell3. To prevent this issue use this code (C# example):

var words = doc.DocumentNode?.SelectNodes("//body//text()")?.Select(x => x.InnerText);
return words != null ? string.Join(" ", words) : String.Empty;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号