伪爬虫:实现CSDN博客访问人数统计功能

实现一个功能,博客统计功能

要求:获取自己所关注的博主的10个博客属性,并生成直观性的统计图表。

 

类似这样

time[2020年 05月 04日 星期一 00:38:04 CST]我有必要写这么一个功能出来,尽快编写出来.

 

update

(update 2020年 05月 25日 星期一 01:47:44 CST)

实现思路:先获取到网页内容,再通过网页内容截取到博客对应属性值。

这是源博客首页对应属性值的展示块前端源代码:

截取到博客对应属性值,是通过使用正则表达式的傻办法来获取的,下面是使用php实现这个功能的源码。

<?php
/**
 * Created by PhpStorm.
 * User: wangyetao
 * Date: 20-5-25
 * Time: 上午1:14
 */

function findNum($str = '')
{
    $str = trim($str);
    if (empty($str)) {
        return '';
    }
    $reg = '/(\d{1}(\d+)?)/is';//匹配数字的正则表达式
    preg_match_all($reg, $str, $result);
    if (is_array($result) && !empty($result) && !empty($result[1]) && !empty($result[1][0])) {
        return $result[1][0];
    }
    return '';
}

$blog_arr = array([
    "https://blog.csdn.net/u014132947",
    "https://blog.csdn.net/czh500",
]);


for ($i = 0; $i < sizeof($blog_arr[0]); $i++) {
    $txt = file_get_contents($blog_arr[0][$i]);
    $isMatched = preg_match_all('/title="(\d+|\d+(.*)\S)"/', $txt, $matches);

    echo $blog_arr[0][$i]."<br>";
    echo "时间:" . date('Y/m/d H:i:s') . "<br>";
    echo "原创:" . findNum($matches[0][0]) . "<br>";
    echo "粉丝:" . findNum($matches[0][1]) . "<br>";
    echo "获赞:" . findNum($matches[0][2]) . "<br>";
    echo "评论:" . findNum($matches[0][3]) . "<br>";
    echo "访问:" . findNum($matches[0][4]) . "<br>";
    echo "积分:" . findNum($matches[0][5]) . "<br>";
    echo "收藏:" . findNum($matches[0][6]) . "<br>";
    echo "周排名:" . findNum($matches[0][7]) . "<br>";
    echo "总排名:" . findNum($matches[0][8]) . "<br>";
    echo "等级:" . findNum($matches[0][9]) . "<br><br>";
}

程序运行结果:

 

ok,over(先写到这儿,图表功能后续完善,2020年 05月 25日 星期一 02:10:50 CST)

 

 

update2

(update 2020年 05月 25日 星期一 22:36:11 CST)

<?php
/**
 * Created by PhpStorm.
 * User: wangyetao
 * Date: 20-5-25
 * Time: 下午10:37
 */

function findNum($str = '')
{
    $str = trim($str);
    if (empty($str)) {
        return '';
    }
    $reg = '/(\d{1}(\d+)?)/is';//匹配数字的正则表达式
    preg_match_all($reg, $str, $result);
    if (is_array($result) && !empty($result) && !empty($result[1]) && !empty($result[1][0])) {
        return $result[1][0];
    }
    return '';
}

$blog_arr = array([
    "https://blog.csdn.net/u014132947",
    "https://blog.csdn.net/mr_hansir",
    "https://blog.csdn.net/stpeace",
    "https://blog.csdn.net/qq_26230421",
    "https://blog.csdn.net/weixin_42429718",
    "https://blog.csdn.net/qq_32392597",
    "https://blog.csdn.net/qq_35190492",
    "https://blog.csdn.net/qq_17623363",
    "https://blog.csdn.net/weixin_34255055",
    "https://blog.csdn.net/pandajava",
    "https://blog.csdn.net/weixin_44339238",
    "https://blog.csdn.net/zll_0405",
    "https://blog.csdn.net/qq_44762698",
    "https://blog.csdn.net/littletigerat",
    "https://blog.csdn.net/a516972602",
    "https://blog.csdn.net/jx950915",
    "https://blog.csdn.net/u011067966",
    "https://blog.csdn.net/u012052268",
    "https://blog.csdn.net/fireroll",
    "https://blog.csdn.net/u014132947",
    "https://blog.csdn.net/czh500",
    "https://blog.csdn.net/ashimar_a",
    "https://blog.csdn.net/u011067966",
    "https://blog.csdn.net/u012052268",
    "https://blog.csdn.net/fireroll",
    "https://blog.csdn.net/u014132947",
    "https://blog.csdn.net/czh500",
    "https://blog.csdn.net/ashimar_a",
]);


echo "<table border='6'><tr>
    <td width=\"14%\" bgcolor='gray'>博客</td>
    <td width=\"14%\" bgcolor='gray'>时间</td>
    
    <td width=\"6%\" bgcolor='gray'>原创</td>
    <td width=\"6%\" bgcolor='gray'>粉丝</td>
    
    <td width=\"6%\" bgcolor='gray'>获赞</td>
    <td width=\"6%\" bgcolor='gray'>评论</td>
    
    <td width=\"6%\" bgcolor='gray'>访问</td>
    <td width=\"6%\" bgcolor='gray'>积分</td>
    
    <td width=\"6%\" bgcolor='gray'>收藏</td>
    <td width=\"6%\" bgcolor='gray'>周排名</td>
    
    <td width=\"6%\" bgcolor='gray'>总排名</td>
    <td width=\"6%\" bgcolor='gray'>等级</td>
</tr>";


$starttime = explode(' ',microtime());
for ($i = 0; $i < sizeof($blog_arr[0]); $i++) {
    $txt = file_get_contents($blog_arr[0][$i]);
    $isMatched = preg_match_all('/title="(\d+|\d+(.*)\S)"/', $txt, $matches);

    echo "<tr>";
    echo '<td width=\"14%\">'.$blog_arr[0][$i].'</td>';
    echo "<td width=\"14%\">".date('Y/m/d H:i:s')."</td>";
    echo "<td width=\"6%\">".findNum($matches[0][0])."</td>";
    echo "<td width=\"6%\">".findNum($matches[0][1])."</td>";
    echo "<td width=\"6%\">".findNum($matches[0][2])."</td>";
    echo "<td width=\"6%\">".findNum($matches[0][3])."</td>";
    echo "<td width=\"6%\">".findNum($matches[0][4])."</td>";
    echo "<td width=\"6%\">".findNum($matches[0][5])."</td>";
    echo "<td width=\"6%\">".findNum($matches[0][6])."</td>";
    echo "<td width=\"6%\">".findNum($matches[0][7])."</td>";
    echo "<td width=\"6%\">".findNum($matches[0][8])."</td>";
    echo "<td width=\"6%\">".findNum($matches[0][9])."</td>";
    echo "</tr>";
}
echo "</table>";

//程序运行时间
$endtime = explode(' ',microtime());
$thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);
$thistime = round($thistime,3);
echo "本网页执行耗时:".$thistime." 秒。".time();

程序运行结果:

 

update3

(update 2020年 06月 01日 星期一 02:14:39 CST);update3程序源码附件,点击下载

<?php
/**
 * Created by PhpStorm.
 * User: wangyetao
 * Date: 20-5-31
 * Time: 下午11:20
 */


function findNum($str = '')
{
    $str = trim($str);
    if (empty($str)) {
        return '';
    }
    $reg = '/(\d{1}(\d+)?)/is';//匹配数字的正则表达式
    preg_match_all($reg, $str, $result);
    if (is_array($result) && !empty($result) && !empty($result[1]) && !empty($result[1][0])) {
        return $result[1][0];
    }
    return '';
}
function findFollowLinks($str = '')
{
    $str = trim($str);
    $arrays = array();
    $follows = array();
    $follows_2 = array();
    $array_n = array();

    preg_match_all('/https:\\/\\/me.csdn.net\\/\s*\S+?\"/', $str, $arrays);
    // $follows去重,替换 'me'为'blog',提取链接
    $follows = $arrays[0];
    $follows_2 = array_unique($follows);
    $follows_2 =array_merge($array_n,$follows_2);

    for ($i = 0; $i < sizeof($follows_2); $i++) {
        $follows_2[$i] = $follows_2[$i];
        //替换
        $follows_2[$i] = str_replace('"', '', $follows_2[$i]);
        $follows_2[$i] = str_replace("me", "blog", $follows_2[$i]);
    }
    //var_dump($follows_2);exit;

    return $follows_2;
}
//输出统计结果
function outputStatistics($blog,$follows){
    echo "<table border='6'>
    <tr><td width100%\" colspan=\"12\" bgcolor='gray'>博主($blog)所关注的统计结果</td></tr>
    <tr>
        <td width=\"14%\" bgcolor='gray'>博客</td>
        <td width=\"14%\" bgcolor='gray'>时间</td>
        
        <td width=\"6%\" bgcolor='gray'>原创</td>
        <td width=\"6%\" bgcolor='gray'>粉丝</td>
        
        <td width=\"6%\" bgcolor='gray'>获赞</td>
        <td width=\"6%\" bgcolor='gray'>评论</td>
        
        <td width=\"6%\" bgcolor='gray'>访问</td>
        <td width=\"6%\" bgcolor='gray'>积分</td>
        
        <td width=\"6%\" bgcolor='gray'>收藏</td>
        <td width=\"6%\" bgcolor='gray'>周排名</td>
        
        <td width=\"6%\" bgcolor='gray'>总排名</td>
        <td width=\"6%\" bgcolor='gray'>等级</td>
    </tr>";


    $starttime = explode(' ', microtime());
    for ($i = 0; $i < sizeof($follows); $i++) {
        $text = file_get_contents($follows[$i]);
        preg_match_all('/title="(\d+|\d+(.*)\S)"/', $text, $matches);

        echo "<tr>";
        echo '<td width=\"14%\">' . $follows[$i] . '</td>';
        echo "<td width=\"14%\">" . date('Y/m/d H:i:s') . "</td>";
        echo "<td width=\"6%\">" . findNum($matches[0][0]) . "</td>";
        echo "<td width=\"6%\">" . findNum($matches[0][1]) . "</td>";
        echo "<td width=\"6%\">" . findNum($matches[0][2]) . "</td>";
        echo "<td width=\"6%\">" . findNum($matches[0][3]) . "</td>";
        echo "<td width=\"6%\">" . findNum($matches[0][4]) . "</td>";
        echo "<td width=\"6%\">" . findNum($matches[0][5]) . "</td>";
        echo "<td width=\"6%\">" . findNum($matches[0][6]) . "</td>";
        echo "<td width=\"6%\">" . findNum($matches[0][7]) . "</td>";
        echo "<td width=\"6%\">" . findNum($matches[0][8]) . "</td>";
        echo "<td width=\"6%\">" . findNum($matches[0][9]) . "</td>";
        echo "</tr>";
    }
    echo "</table>";

//程序运行时间
    $endtime = explode(' ', microtime());
    $thistime = $endtime[0] + $endtime[1] - ($starttime[0] + $starttime[1]);
    $thistime = round($thistime, 3);
    echo "本网页执行耗时:" . $thistime . " 秒。" . time();
}

// 待统计的主博客地址
//$blog = "https://me.csdn.net/follow/u014132947";
$blog ="https://me.csdn.net/follow/zengfanwei1990";
//$blog ="https://me.csdn.net/follow/weixin_40908748";
$txt = file_get_contents($blog);
$follows = array();
$follows = findFollowLinks($txt);
outputStatistics($blog,$follows);



程序运行结果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章