7

我正在做一个项目,我必须根据该页面的 URL 找出该页面的关键字密度。我用谷歌搜索了很多,但没有找到帮助和脚本,我找到了一个付费工具http://www.selfseo.com/store/_catalog/php_scripts/_keyword_density_checker_php_script

但我实际上并不知道“页面的关键字密度”实际上是什么意思?还请告诉我如何创建一个 PHP 脚本来获取网页的关键字密度。

谢谢

4

5 回答 5

24

“关键字密度”只是单词出现的频率,以单词总数的百分比给出。以下 PHP 代码将输出字符串中每个单词的密度,$str. 它表明关键字密度不是一个复杂的计算,它可以在几行 PHP 中完成:

<?php
$str = "I am working on a project where I have to find out the keyword density of the page on the basis of URL of that page. But I am not aware actually what \"keyword Density of a page\" actually means? and also please tell me how can we create a PHP script which will fetch the keyword density of a web page.";

// str_word_count($str,1) - returns an array containing all the words found inside the string
$words = str_word_count(strtolower($str),1);
$numWords = count($words);

// array_count_values() returns an array using the values of the input array as keys and their frequency in input as values.
$word_count = (array_count_values($words));
arsort($word_count);

foreach ($word_count as $key=>$val) {
    echo "$key = $val. Density: ".number_format(($val/$numWords)*100)."%<br/>\n";
}
?>

示例输出:

of = 5. Density: 8%
a = 4. Density: 7%
density = 3. Density: 5%
page = 3. Density: 5%
...

要获取网页的内容,您可以使用file_get_contents(或cURL)。例如,以下 PHP 代码列出了该网页上所有高于 1% 密度的关键字:

<?php
$str = strip_tags(file_get_contents("http://stackoverflow.com/questions/819166"));

$words      = str_word_count(strtolower($str),1);
$word_count = array_count_values($words);

foreach ($word_count as $key=>$val) {
    $density = ($val/count($words))*100;
    if ($density > 1)
        echo "$key - COUNT: $val, DENSITY: ".number_format($density,2)."%<br/>\n";
}
?>

我希望这有帮助。

于 2010-03-22T13:58:00.867 回答
5

或者你可以试试这个:http
://code.eyecatch-up.de/?p=155更新:将课程重新定位到http://code.google.com/p/php-class-keyword-density-check/

<?php
include 'class/class.keywordDensity.php';             // Include class  

$obj = new KD();                                      // New instance
$obj->domain = 'http://code.eyecatch-up.de';          // Define Domain
print_r ($obj->result()); 
?>

上面的代码返回:

Array
(
    [0] => Array
        (
            [total words] => 231
        )

    [1] => Array
        (
            [keyword] => display
            [count] => 14
            [percent] => 6.06
        )
and so on...

适用于本地和远程文件。

于 2010-10-28T15:32:50.317 回答
2

关键字密度大致为:

(页面上出现关键字的次数)/(其他关键字的总数)

于 2009-06-07T10:32:15.030 回答
1

关键字密度仅表示关键字出现在内容中相对于文本其余部分的百分比。一般来说,它也是一个相当无用的 SEO 指标。我不会费心为它构建脚本,因为你最好专注于其他指标。您可能会发现此参考很有用。

于 2009-05-04T07:56:13.063 回答
0

如果给定的关键字是“大象行走”,则关键字密度将是术语“大象行走”相对于其他文本出现在任何给定网页上的频率。正如 VirtuosiMedia 所说,这是(广泛)无用的信息。

要衡量它,您必须从文本中去除所有标记,计算单词同时跟踪关键字出现的频率。

到那时,您就会知道,该文本中所有单词的 xx.xx % 是关键字。xx.xx % 的时间,关键字彼此相邻使用,因此“大象散步”的关键字密度是 xx

同样,这很有用的唯一原因是在 php.ini 中演示模式匹配和字符串函数。

于 2009-05-04T08:03:09.927 回答