PHP头条
热点:

php采集csdn首页新闻


 

<?php

 

function csdn(){//$uid采集文章的分类 

    $url="http://www.csdn.net"; 

    $ch = curl_init(); 

    curl_setopt ($ch, CURLOPT_URL,$url); 

    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 

    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);     

    curl_setopt($ch,CURLOPT_ENCODING ,'utf8');  

    $content = curl_exec($ch); 

    preg_match_all("/http\:\/\/\w*\.csdn\.net\/a\/\d*\/\d*\.html/",$content,$match); 

    $weburl=$match[0]; 

    $weburl=array_unique($weburl); 

    $j=0; 

    foreach($weburl as $i=>$vo){ 

        curl_setopt ($ch, CURLOPT_URL,$vo); 

        $content = curl_exec($ch); 

        preg_match_all("/\<h1\>(.*)\<\/h1\>|\<div\s*class\=\"blkCont.*([\s\S]*)\<div\s*class\=\"page\".*\>/",$content,$match); 

        if(!emptyempty ( $match[2][1])){ 

            $list[$j]['content']=$match[2][1]; 

            $list[$j]['title']=$match[1][0]; 

            $j++; 

        } 

    } 

    print_r($list); 

?> 

 

很容易看出

$list就是收集到的新闻,形式是一个二维数组

如果要把他保存到你的数据库,我就不解释了...

其中注意判断是否与你数据库的文章重复

可以通过md5加密标题然后与你数据库的文章标题md5加密后比对,若真.,则表示你数据库有同样的文章

注意这里希望大家复制不要手工复制,请查看源代码方法复制.....

因为表面的代码跟实际代码貌似有出入

 

 

如果你是thinkphp的话..www.2cto.com..那就跟我的一样了...下面贴出更方便的代码直接添加数据库,包括重复数据判断:

 

<?php 

function csdn($uid){//$uid采集文章的分类 

    $url="http://www.csdn.net"; 

    $ch = curl_init(); 

    curl_setopt ($ch, CURLOPT_URL,$url); 

    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 

    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);     

    curl_setopt($ch,CURLOPT_ENCODING ,'utf8');  

    $content = curl_exec($ch); 

    preg_match_all("/http\:\/\/\w*\.csdn\.net\/a\/\d*\/\d*\.html/",$content,$match); 

    $weburl=$match[0]; 

    $weburl=array_unique($weburl); 

    $j=0; 

    foreach($weburl as $i=>$vo){ 

        curl_setopt ($ch, CURLOPT_URL,$vo); 

        $content = curl_exec($ch); 

        preg_match_all("/\<h1\>(.*)\<\/h1\>|\<div\s*class\=\"blkCont.*([\s\S]*)\<div\s*class\=\"page\".*\>/",$content,$match); 

        if(!emptyempty($match[2][1])){ 

            $list[$j]['content']=$match[2][1]; 

            $list[$j]['title']=$match[1][0]; 

            $j++; 

        } 

    } 

    $db=M('news'); 

    $news=$db->where("uid=".$uid)->select(); 

    $flag=true; 

    foreach($list as $i=>$vo){ 

        foreach($news as $j=>$value){ 

            if(md5($value['title'])==md5($vo['title'])){ 

                $flag=false; 

                break; 

            } 

        } 

        if($flag){ 

            $vo['uid']=$uid; 

            $vo['date']=date('Y-j-m H:i:s'); 

            $vo['author']=Session::get("admin"); 

            $vo['iscommand']=1; 

            $rs=$db->add($vo); 

        } 

        $flag=true; 

    } 

?>   

 

摘自 zouhao619的专栏

www.phpzy.comtrue/phprm/19343.htmlTechArticlephp采集csdn首页新闻 ?php function csdn(){//$uid采集文章的分类 $url=http://www.csdn.net; $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL,$url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIME...

相关文章

    暂无相关文章

PHP之友评论

今天推荐