zoukankan      html  css  js  c++  java
  • [小黑科技] 分享一个可以节省你30分钟的博客园个人博客目录自动生成引擎(过来试玩)

    一、大背景 

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

    [Beautifulzzzz的博客目录] 快速索引点这儿O(∩_∩)O~~,红色标记的是不错的(⊙o⊙)哦~

    下面整理的目录截图:

    二、问题所在

    手动更新目录存在以下几点问题:

    ① 每次写完文章去更新目录是个很繁琐的事情,一般人做着做着就放弃了(比如我,只更新到14年)

    ② 优质美观的目录(整齐)需要直接用HTML编辑,如果不用HTML可能突然某个地方格式很奇葩,如果不用HTML调美观很费功夫

       

    ③ 即使使用HTML编辑,由于博客园的HTML编辑框太简单,没有查找、替换、格式化、折叠,插入新条目也颇费劲

     

    三、问题解决(在这试玩)

    针对手动更新的问题,爱偷懒的博主尝试写了一个目录自动更新的引擎,现分享给大家:

    http://letitagif.applinzi.com/

    用法:只需将自己博客的用户名输入搜索框

    前提:你的博客主页有随笔分类这一栏

    举例:例如我的博客的用户名为zjutlitao,因此搜索框内输入zjutlitao,点击搜索随机会生成你的目录

       

    四、实现思想

    ① 爬取主页HTML源码

    ② 解析侧栏随笔分类列表,获取所有随笔分类及对应URL

    ③ 利用上面解析的URL,爬取每种分类的HTML源码

    ④ 解析每种分类下面的文章列表

    ⑤ 将生成的目录输出

    核心源码如下:

     1 <?php
     2 
     3     include "php-simple-html-dom-parser-master/Src/Sunra/PhpSimple/HtmlDomParser.php";
     4     use SunraPhpSimpleHtmlDomParser;
     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}sd{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 

  • 相关阅读:
    bbb SOCKET CAN
    在BBB上使用CCS5开发环境
    BBB的PRU模块
    垃圾邮件分类
    yzoj 2377 颂芬梭哈 题解
    yzoj 2372 小B的数字 题解
    yzoj P2371 爬山 题解
    hdu 1007 Quoit Design 题解
    手写堆
    yzoj P1126 塔 题解
  • 原文地址:https://www.cnblogs.com/zjutlitao/p/5774927.html
Copyright © 2011-2022 走看看