實現一個功能,博客統計功能
要求:獲取自己所關注的博主的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();
程序運行結果: