Home  >  Article  >  php教程  >  PHP采集CSDN博客边栏的阅读排行

PHP采集CSDN博客边栏的阅读排行

WBOY
WBOYOriginal
2016-06-16 09:16:221042browse

PHP采集CSDN博客边栏的阅读排行

  项目中要用到采集的数据,所以就先拿CSDN博客来试了试。这里使用Simple HTML DOM(官网)这个库,它能够方便的遍历HTML文档。

<?php
    include_once('simple_html_dom.php');
    header('Content-Type:text/html;charset=utf-8');
    $html = file_get_html('http://blog.csdn.net/szy361');

    $res = $html->find('#hotarticls ul.panel_body li a[title]');//取得id=hotarticls下class为panel_bodya的ul标签下的a的title
    $span = $html->find('#hotarticls ul.panel_body li span');//取得span
    foreach($res as $element){
        $arr[] = $element->title.'&#43;'.$element->href;//将title&#20540;和href的&#20540;通过&#43;连起来
    }
    foreach($span as $e){
        $brr[] = $e->innertext;//得到span下的&#20540;组成的数组
    }
    
    //将两个数组组成一个新的二维数组
    for($i=0;$i<count($res);$i&#43;&#43;){
        $crr[] = explode('&#43;',$arr[$i]);
        $crr[$i][] = $brr[$i];
    }
    return $crr;

扩展:

PHP Simple HTML DOM解析器使用入门

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn