遍历所有元素,并得到文本?遍历、元素、文本

2023-09-07 16:53:31 作者:TF发光闪瞎你狗眼

我现在用的是后续code,以得到一个页面的所有文字变成名单,其中,串>

I am using the follow code to get all text from a page into a List<string>

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(content);

foreach (var script in doc.DocumentNode.Descendants("script").ToArray())
    script.Remove();

foreach (var style in doc.DocumentNode.Descendants("style").ToArray())
    style.Remove();

foreach (HtmlAgilityPack.HtmlNode node in doc.DocumentNode.SelectNodes("//text()"))
{
    string found = WebUtility.HtmlDecode(node.InnerText.Trim());
    if (found.Length > 2) // removes some unwanted strings
        query[item.Key].Add(found);
}

但是,一些HTML仍然会到字符串,如&LT; /形式GT; 是 有没有更好的方式来缩小这个code,所以我得到的每一个只有文字 标签并没有别的否则我将不得不结果还是解析到 删除&LT; *?>标记

But some html is still going into the string such as </form> is there a better way to narrow this code so I get only the text of each tag and nothing else or I will have to still parse the results to remove <*> tags ?

推荐答案

这是可以做到相当容易地只使用列入HAP功能。

This can be done rather easily using only functions included in the HAP.

HtmlDocument doc = new HtmlWeb().Load("http://www.google.com");
List<string> words = doc.DocumentNode.DescendantNodes()
        .Where(n => n.NodeType == HtmlNodeType.Text
          && !string.IsNullOrWhiteSpace(HtmlEntity.DeEntitize(n.InnerText))
          && n.ParentNode.Name != "style" && n.ParentNode.Name != "script")
        .Select(n => HtmlEntity.DeEntitize(n.InnerText).Trim())
        .Where(s => s.Length > 2).ToList();

结果的话有超过2,一切都已经逃脱了长的列表,所以不需要 WebUtility