URL 关键字密度检查器 API

URL Keyword density checker API

是否有任何免费的 API 来检查 url(或 html 文档)的关键字密度?我需要它也适用于非英语 urls。

这是关键字密度检查器的示例,但没有 api: http://tools.seobook.com/general/keyword-density/

如果没有免费的 API,C# 库也可以作为我的替代解决方案。

已更新

这是https://dotnetfiddle.net/g40YQ3

我不确定是否有免费的 API,但在 C# 中实现它非常简单。

请求 HTML.

using (WebClient client = new WebClient())
{
    string html = client.DownloadString("http://whosebug.com/").ToLower();
}

使用正则表达式删除 HTML 个元素。您可以修改它以删除其他元素,例如 <style> 元素只需添加带有 | 字符的 <style[^>]*>[\s\S]*</style>

public static string RemoveHtmlTags(string html)
{
    string htmlRemoved = Regex.Replace(html, @"<script[^>]*>[\s\S]*?</script>|<[^>]+>|&nbsp;", " ").Trim();
    string normalised = Regex.Replace(htmlRemoved, @"\s{2,}", " ");
    return normalised;
}

//remove html elements
html = RemoveHtmlTags(html);

用空格分割字符串

List<string> list = htmlCode.Split(' ').ToList();

使用正则表达式删除非字母字符 - 可选

var onlyAlphabetRegEx = new Regex(@"^[A-z]+$");
list = list.Where(f => onlyAlphabetRegEx.IsMatch(f)).ToList();

更多黑名单词(大于 2 个字符,不重要等)- 可选

//add your own
string[] blacklist = { "a", "an", "on", "of", "or", "as", "i", "in", "is", "to", "the", "and", "for", "with", "not", "by" };
list = list.Where(x => x.Length > 2).Where(x => !blacklist.Contains(x)).ToList();

然后通过key和count得到distinct关键字,然后通过count排序。

var keywords = list.GroupBy(x => x).OrderByDescending(x => x.Count());

foreach (var word in keywords)
{
    Console.WriteLine("{0} {1}", word.Key, word.Count());
}

这是一个非常简单的示例,说明如何找到关键字密度,可以进一步改进。

还有一个名为 HtmlAgilityPack 的库,它在一定程度上有助于处理 html 个元素。

希望对您有所帮助。