[小黑科技] 分享一個能夠節省你30分鐘的博客園我的博客目錄自動生成引擎(過來試玩)

 

1、大背景 php

常常在博客園寫博客的博主,文章寫多了就想着整理整理文章。下面是我在14年11月整理的一篇:html

[Beautifulzzzz的博客目錄] 快速索引點這兒O(∩_∩)O~~,紅色標記的是不錯的(⊙o⊙)哦~git

下面整理的目錄截圖:github

 

2、問題所在正則表達式

手動更新目錄存在如下幾點問題:mvc

① 每次寫完文章去更新目錄是個很繁瑣的事情,通常人作着作着就放棄了(好比我,只更新到14年)app

② 優質美觀的目錄(整齊)須要直接用HTML編輯,若是不用HTML可能忽然某個地方格式很奇葩,若是不用HTML調美觀很費功夫dom

   

③ 即便使用HTML編輯,因爲博客園的HTML編輯框太簡單,沒有查找、替換、格式化、摺疊,插入新條目也頗費勁ide

 

 

3、問題解決(在這試玩)post

針對手動更新的問題,愛偷懶的博主嘗試寫了一個目錄自動更新的引擎,現分享給你們:

http://letitagif.applinzi.com/

用法:只需將本身博客的用戶名輸入搜索框

前提:你的博客主頁有隨筆分類這一欄

舉例:例如個人博客的用戶名爲zjutlitao,所以搜索框內輸入zjutlitao,點擊搜索隨機會生成你的目錄

   

 

4、實現思想

① 爬取主頁HTML源碼

② 解析側欄隨筆分類列表,獲取全部隨筆分類及對應URL

③ 利用上面解析的URL,爬取每種分類的HTML源碼

④ 解析每種分類下面的文章列表

⑤ 將生成的目錄輸出

核心源碼以下:

 1 <?php
 2 
 3     include "php-simple-html-dom-parser-master/Src/Sunra/PhpSimple/HtmlDomParser.php";
 4     use Sunra\PhpSimple\HtmlDomParser;
 5     
 6     $user_name = $_GET["search_user_name"];
 7 
 8     $main_url = "http://www.cnblogs.com/$user_name/mvc/blog/sidecolumn.aspx?blogApp=$user_name";
 9     $main_dom = HtmlDomParser::file_get_html($main_url);
10     $kind_list_dom = $main_dom->find('.catListPostCategory');
11 
12     $time_reg="/\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}/";//匹配時間的正則表達式
13     $goal_reg="/\(([0-9]+)\)/";//匹配(XXX)的正則表達式
14 
15 
16 
17     foreach($kind_list_dom[0]->find('a') as $a_kind_dom){// Find all kind links                 
18         $a_kind_url = $a_kind_dom->href;
19         $a_kind_name = $a_kind_dom->innertext;
20         $out_dom = "<p class='beautifulzzzz_p'><span><strong>" .$a_kind_name ."</strong></span></p>";
21         
22      
23         $a_kind_dom = HtmlDomParser::file_get_html( $a_kind_url );
24         foreach($a_kind_dom->find('.entrylistItem') as $element){//全部內容條目都是class=entrylistItem
25            $item=$element->children(0)->children(0);
26            $item->id="beautifulzzzz_id";
27            $item->class="beautifulzzzz_class";
28            $item->target="_blank";
29            
30            $info=$element->children(2);
31            $info_string=$info->plaintext;
32 
33            /*
34            匹配時間,文章列表最下面的文章發佈時間、閱讀量等信息
35            if(preg_match($time_reg,$info_string,$time_out)){//匹配時間
36                $time_out[0];
37            }else{
38                $out_dom = $out_dom  ."<span>" .($item->outertext) ."</br></span>";//輸出內容    
39            }
40            */
41            
42            /*
43            匹配閱讀量+評論量
44            if(2==preg_match_all($goal_reg,$info_string,$goal_out)){//匹配閱讀量等
45                echo $info_string .$goal_out[0][0] .$goal_out[0][1];
46            }else{
47                $out_dom = $out_dom  ."<span>" .($item->outertext) ."</br></span>";//輸出內容    
48            }
49            */
50            
51            /*根據閱讀量將閱讀量超過500或評論超過2的文章設置爲紅色*/
52            
53            if(2==preg_match_all($goal_reg,$info_string,$goal_out)){//匹配閱讀量等
54                $read_number=(int)substr($goal_out[0][0],1,strlen($goal_out[0][0])-2);
55                $say_number=(int)substr($goal_out[0][1],1,strlen($goal_out[0][1])-2);
56                
57                //echo $read_number ."   " .$say_number;
58                if($read_number>=500 || $say_number>=2){
59                    $item->class="beautifulzzzz_class_important";
60                }
61                $out_dom = $out_dom  ."<span>" .($item->outertext) ."</br></span>";//輸出內容
62              
63            }
64         }
65       ob_start();
66       echo $out_dom;
67       ob_end_flush();
68     }/**/
69 ?>

 

 


參考連接

http://www.cnblogs.com/txw1958/archive/2013/01/19/2867584.html
https://github.com/sunra/php-simple-html-dom-parser DOM解析的開源項目
http://simplehtmldom.sourceforge.net/manual.htm 上面開源DOM解析的說明API文檔,頗有用
http://www.cnblogs.com/jbexploit/p/4592527.html#3212750
http://www.educity.cn/help/HTMLCSS/200801141504481718.htm HTML與CSS入門:內部樣式表和內聯樣式
http://blog.csdn.net/wolinxuebin/article/details/7615098 CSS 元素垂直居中的 6種方法
http://www.w3schools.com/js/ Javascript介紹,英文
http://www.ziqiangxuetang.com/jsref/prop-button-name.html button和js綁定
關於PHP"ECHO"輸出效率問題? 
加速PHP的ECHO 
關於時間正則表達式 
PHP 正則表達式查找字符串 
上面兩個看着沒作出來,浪費很長時間,最後仔細比對才發現反斜槓搞錯了,這裏資料比較全


 

 

@beautifulzzzz - 物聯網&普適計算實踐者
e-mail:beautifulzzzz@qq.com 
i-blog:blog.beautifulzzzz.com 

相關文章
相關標籤/搜索