僞爬蟲:實現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);



程序運行結果:

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