A-A+
PHP采集小程序-YAHOO笑话采集、分页采集
php写的一个小程序,只有下面的源码,即可以将其文章内容采集下来,可以显示采集网址以及链接,采集标题和内容,主要是利用正则原理进行采集,虽然没有进行入库,但如果采集数据不是很大,比如单页采集便可以使用此小程序了,可以直接进行php采集预览,也可以更改规则,运行一下吧,很有意思。
<?php // 采集首页地址 function yahoo_xh($url= "http://cn.jokes.yahoo.com/jok/index.html") { // 获取页面代码 $r = file_get_contents($url); // 设置匹配正则 $preg = "{hspace=5><a href=\"http://cn.jokes.yahoo.com/(.*).h tml\" class=list target=_blank>}isU"; // 进行正则搜索 preg_match_all($preg, $r, $title); // 计算标题数量 $count = count($title[0]); // 通过标题数量进行内容采集 for($i=0;$i<$count;$i++) { preg_match("/http(.*)html/U",$title[0][$i],$jurl); echo "<a href=".$jurl[0]." target='_blank'>".$jurl[0]."</a>"; //获取详细地址 $c = @file_get_contents($jurl[0]); $preg1="{<h1 id=\"heading\">(.*)</h1>}isU"; $preg2="{<div id=\"newscontent\">(.*)</div>}isU"; preg_match_all($preg1,$c,$titles); //标题 preg_match_all($preg2,$c,$content);//内容 echo "<h1>".$titles[0][0]."</h1><br/>"; echo "".$content[0][0]."<hr/>"; } } yahoo_xh(); yahoo_xh("http://cn.jokes.yahoo.com/jok/index1.html"); yahoo_xh("http://cn.jokes.yahoo.com/jok/index2.html"); yahoo_xh("http://cn.jokes.yahoo.com/jok/index3.html"); ?>
似乎没有体现出分类采集啊。
有思路么