Home  >  Article  >  php教程  >  使用simple_html_dom抓取oschina的新闻资讯

使用simple_html_dom抓取oschina的新闻资讯

PHP中文网
PHP中文网Original
2016-05-25 17:10:481120browse

感觉网站太空洞了,就找了找平时自己浏览的IT资讯网站,将内容抓取过来,这样就不能每个网站都浏览了.

   
  //标题+预览 
   public function to_oschina(){
        $datas=$this->_comm_oschina();
        $this->mylib->My_show_list(mymsg::PZ_OSCHINA, 'pc/oschina',$datas,null,'no desc',5,$this->pagesize);    
    }  
   //仅标题
     public function to_oschina_title(){
        $datas=$this->_comm_oschina();
        $this->mylib->My_show_list(mymsg::PZ_OSCHINA, 'pc/oschina_title',$datas,null,'no desc',5,$this->pagemax);    
    } 
   //在页面停留指定秒后开始抓取数据
    function _comm_oschina(){
        $datas=$this->mytool->getInfo();
        $datas["url"]="http://www.oschina.net";
        $datas["ajaxMethod"]="get_oschina";
        $datas["funMethod"]="to_oschina";
        return $datas;     
    } 
  
   //前台用ajax进行调用 
    function get_oschina(){  
      $this->_insert_oschina();
    }

  function _insert_oschina(){

         $url="http://www.oschina.net/news/list";
          $html = file_get_html($url);
          $result = $html->find('.ArticleList ul li');
          
          for($j=0;$jfind("td");
            for($i=0;$ifind("h3 a",$i);
             if(!empty($h)){
                $data["title"]=$h->innertext;   //标题
                $url=$h->href;
                $data["uri"]= str_replace("http://www.oschina.net","",$url);;          //URI地址
             }

             $p=$detail[$i]->find(".date",$i);
             if(!empty($p)){
                $data["author_url"]=$p->find('a',$i)->href; //作者地址
                $data["author_nm"]=$p->find('a',$i)->innertext; //作者名称
                $data["time_detail"]=$p->innertext();     //作者发布信息
                $data["no"]= substr($data["time_detail"],strrpos($data["time_detail"],"(")+1,16);
             }
             $d=$detail[$i]->find(".detail",$i);
              if(!empty($p)){
                $data["info"]=$d->innertext;    //简介
              }
            }
           $bool=$this->mcom_model->checkIsExit(mymsg::PZ_OSCHINA,array("title"=>$data["title"]));
            if(!$bool){
               $this->mcom_model->insert_data(mymsg::PZ_OSCHINA,$data);
            }
        }  
    }


这样就能抓取oschina的资讯保存进数据库,刷新后可查看

    查看实例:http://www.xiuxiandou.com/xitong/pachong/to_oschina
       


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