简体   繁体   English

我如何只解析单个html行中的文本?

[英]How can i parse only the text from a single html line?

I have this line: 我有这条线:

<a  onmouseover="EnterContent('ToolTip','לחיילים ולתושבי הדרום באהבה','<u><span style=color:#000099;>כתב: רוטרית   בתאריך: 22.07.14  שעה: 08:56</span></u><br>המון רצון לעזור, להתנדב, להעניק, לפנק, לאהוב, ולחבק קיים היום בעם.<br>נצלו אשכול זה לפרסם דברים שיוכלו לעזור לחיילים ולתושבי הדרום.<br><br>חיילים, ותו...'); Activate();" onmouseout="deActivate()" href="javascript:void(0)"> 

From this line i need to get only the hebrew words. 从这一行中,我只需要了解希伯来语。 To remove all tags and the onmouseover and tooltip and void and only to be left with the words in hebrew and the part: בתאריך: 22.07.14 שעה: 08:56 删除所有标签以及onmouseover和工具提示以及空白,只剩下希伯来语和该部分中的单词:בתאריך:22.07.14שעה:08:56

Or in this case : 或者在这种情况下:

<a  onmouseover="EnterContent('ToolTip','אין לדווח בפורום על תנועת כוחות, סדרי כוחות, פעילות מבצעית וכל דיווח המסכן חיי חיילים','<u><span style=color:#000099;>כתב: מובחר   בתאריך: 17.07.14  שעה: 23:20</span></u><br>[anchor:אשכול עוגן מתאריך  17.07.14 בשעה  23:20  על-ידי  Maya, (גלובל)]במסגרת הכניסה הקרקעית במבצע צוק איתן, ההנהלה פונה אליכם ומבקשת בכל לשון של בקשה...'); Activate();" onmouseout="deActivate()" href="javascript:void(0)"> 

Again to be left with all hebrew words and: מתאריך 17.07.14 בשעה 23:20 再次留下所有希伯来语单词和:מתאריך17.07.14彼得23:20

How can i do it ? 我该怎么做 ?

I have this method i used to parse text: 我有我用来解析文本的这种方法:

public List<string> CreateTextList(string filePath)
        {
            List<string> text = new List<string>();
            var htmlDoc = new HtmlAgilityPack.HtmlDocument();
            htmlDoc.OptionFixNestedTags = true;
            htmlDoc.Load(filePath, System.Text.Encoding.GetEncoding(65001));

            if (htmlDoc.DocumentNode != null)
            {
                var nodes = htmlDoc.DocumentNode.SelectNodes("//a/b");
                foreach (var node in nodes)
                {
                    text.Add(node.InnerText);

                }
            }
            text = Filters.filterNumbers(text);
            return text;
        }

It's working good but it's getting file not lines/text. 它工作正常,但文件而不是行/文本。

Instead of HtmlDocument.Load(string path) use the method HtmlDocument.LoadHtml(string html) : 代替HtmlDocument.Load(string path)使用方法HtmlDocument.LoadHtml(string html)

string html = "<a  onmouseover=\"EnterContent('ToolTip....";
var htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(html);

Well, you can't use an XML parser if you work with lines (you can't traverse the XML tree structure if you don't have the whole structure). 好吧,如果您使用行,则不能使用XML解析器(如果没有整个结构,则不能遍历XML树结构)。

But as suggested here: https://stackoverflow.com/a/19524158/1648371 但如此处建议的那样: https : //stackoverflow.com/a/19524158/1648371

You can use 您可以使用

string noHTML = Regex.Replace(inputHTML, @"<[^>]+>|&nbsp;", "").Trim();

For retrieving the strings instead of replacing the HTML characters with an empty space, you can replace them with a special character that you won't have in your input (like the Swedish letter å) and then 要检索字符串而不是用空格替换HTML字符,可以将其替换为输入中不会包含的特殊字符(例如瑞典字母å),然后

Regex.Matches(noHTML, "å", RegexOptions.IgnoreCase))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM