Home >Backend Development >PHP Tutorial >PHP collects csdn home page news_PHP tutorial

PHP collects csdn home page news_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:47:48945browse

 

 

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("/

(.*)

|/",$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..那就跟我的一样了...下面贴出更方便的代码直接添加数据库,包括重复数据判断:

 

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("/

(.*)

|/",$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.bkjia.comtruehttp://www.bkjia.com/PHPjc/478488.htmlTechArticle?php function csdn(){//$uid采集文章的分类 $url=http://www.csdn.net; $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL,$url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); cur...
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