首页 > 后端开发 > php教程 > PHP采撷CSDN博客边栏的阅读排行

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-06-13 12:02:02
原创
902 人浏览过

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

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

复制代码
<span   style="max-width:90%">php    </span><span style="color:#0000ff">include_once</span>('simple_html_dom.php'<span style="color:#000000">);    </span><span style="color:#008080">header</span>('Content-Type:text/html;charset=utf-8'<span style="color:#000000">);    </span><span style="color:#800080">$html</span> = file_get_html('http://blog.csdn.net/szy361'<span style="color:#000000">);    </span><span style="color:#800080">$res</span> = <span style="color:#800080">$html</span>->find('#hotarticls ul.panel_body li a[title]');<span style="color:#008000">//</span><span style="color:#008000">取得id=hotarticls下class为panel_bodya的ul标签下的a的title</span>    <span style="color:#800080">$span</span> = <span style="color:#800080">$html</span>->find('#hotarticls ul.panel_body li span');<span style="color:#008000">//</span><span style="color:#008000">取得span</span>    <span style="color:#0000ff">foreach</span>(<span style="color:#800080">$res</span> <span style="color:#0000ff">as</span> <span style="color:#800080">$element</span><span style="color:#000000">){        </span><span style="color:#800080">$arr</span>[] = <span style="color:#800080">$element</span>->title.'+'.<span style="color:#800080">$element</span>->href;<span style="color:#008000">//</span><span style="color:#008000">将title值和href的值通过+连起来</span><span style="color:#000000">    }    </span><span style="color:#0000ff">foreach</span>(<span style="color:#800080">$span</span> <span style="color:#0000ff">as</span> <span style="color:#800080">$e</span><span style="color:#000000">){        </span><span style="color:#800080">$brr</span>[] = <span style="color:#800080">$e</span>->innertext;<span style="color:#008000">//</span><span style="color:#008000">得到span下的值组成的数组</span><span style="color:#000000">    }        </span><span style="color:#008000">//</span><span style="color:#008000">将两个数组组成一个新的二维数组</span>    <span style="color:#0000ff">for</span>(<span style="color:#800080">$i</span>=0;<span style="color:#800080">$i</span>count(<span style="color:#800080">$res</span>);<span style="color:#800080">$i</span>++<span style="color:#000000">){        </span><span style="color:#800080">$crr</span>[] = <span style="color:#008080">explode</span>('+',<span style="color:#800080">$arr</span>[<span style="color:#800080">$i</span><span style="color:#000000">]);        </span><span style="color:#800080">$crr</span>[<span style="color:#800080">$i</span>][] = <span style="color:#800080">$brr</span>[<span style="color:#800080">$i</span><span style="color:#000000">];    }    </span><span style="color:#0000ff">return</span> <span style="color:#800080">$crr</span>;
登录后复制
复制代码

 

扩展:

PHP Simple HTML DOM解析器使用入门

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
输入<html><>后是怎么变成<html></html>的
来自于 1970-01-01 08:00:00
0
0
0
HTML与HTML5的区别
来自于 1970-01-01 08:00:00
0
0
0
HTML总结
来自于 1970-01-01 08:00:00
0
0
0
HTML总结
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板