您的位置:首页 > 编程语言 > PHP开发

一个简单的有分页采集功能的php采集程序--自制的哦

2009-03-20 17:57 896 查看
<?
@set_time_limit(0);  //设置网页运行时间,其中0为不限
//采集首页地址
$url="http://emotion.pclady.com.cn/skills/";
//获取页面代码
$rs=file_get_contents($url);
//设置匹配正则
$preg='/<i/s+class=/"titles/"><a/s+href=/"[^>]+/">(.*)<//a><//i>/i';
//进行正则搜索
preg_match_all($preg,$rs,$title);
//计算标题数量
$count=count($title[0]);
//通过标题数量进行内容采集
for ($i=0;$i<$cout;$i++){
   
     //设置内容页地址
     $pr='/<a/s+href=/"[^>]+/">/isU';
     preg_match_all($pr,$title[0][$i],$jurl);
     $substr=substr($jurl[0][0],9);
     $curl=substr($substr,0,-18);           
    //获取内容页代码
    $c=file_get_contents($curl);
    //设置内容页匹配正则
    $pc='/<div/s+class=/"words/">(.*)<//div>/isU';
    //进行正则匹配搜索
    preg_match_all($pc,$c,$content);
    //内容页分页正则
    $subcpreg='/<a/s+href=/"[^>]+/"/s+class=/'page_num/'>[^<>]+<//a>/i';
    preg_match_all($subcpreg,$c,$subcpage);
    //内容页数
    $cpage_num=count($subcpage[0]);
    if ($cpage_num<=0) {
        //输出标题
        echo $title[0][$i]."<br>";
        //输出内容
        echo $content[0][0]."<br>";
    }else {
         //输出标题
         echo $title[0][$i]."<br>";
        //输出内容
        echo $content[0][0]."<br>";
        for ($f=1;$f<$cpage_num+1;$f++){
            $purl=substr($curl,0,-5);
            $purl=$purl."_".$f.".html";
            $crrs=file_get_contents($purl);
            $pcc='/<div/s+class=/"words/">(.*)<//div>/isU';
            preg_match_all($pcc,$crrs,$contents);
            echo $contents[0][0];           
        }
    }
}
//获取列表分页地址
$subpreg='/<a/s+href=/"[^>]+/"/s+class=/'page_num/'>[^<>]+<//a>/i';//列表页分页正则
preg_match_all($subpreg,$rs,$subpage);
//列表页数
$page_num=count($subpage[0]);
/*$fp=fopen("text.txt","a");
$fw=fwrite($fp,$subpage[0][0]);
fclose($fp);*/
if ($page_num<=0) {  
    //没有列表分页
   echo "当前没有分页";   
}else {   //有列表分页
    for ($j=1;$j<$page_num+1;$j++){
        $purl=$url."/index_".$j.".html";
        $rss=file_get_contents($purl);
        preg_match_all($preg,$rss,$titles);
        $pcount=count($titles[0]);
        for ($k=0;$k<$pcount;$k++){
            //设置内容页地址
           $pr='/<a/s+href=/"[^>]+/">/isU';
           preg_match_all($pr,$titles[0][$k],$jurl);
           $substr=substr($jurl[0][0],9);
           $purl=substr($substr,0,-18);           
          //获取内容页代码
          $ppc=file_get_contents($purl);
        //设置内容页匹配正则
          $ppr='/<div/s+class=/"words/">(.*)<//div>/isU';
        //进行正则匹配搜索
         preg_match_all($ppr,$ppc,$contents);
         //输出标题
         echo $titles[0][$k]."<br>";
        //输出内容
        echo $contents[0][0]."<br>";
        }
        echo "<br>"."<br>";
        $count=$count+$pcount;
    }
}

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php class file fp url rss