Home  >  Article  >  Backend Development  >  thinkphp5 + beanbun realizes simple crawling of movie URLs and pictures

thinkphp5 + beanbun realizes simple crawling of movie URLs and pictures

零到壹度
零到壹度Original
2018-03-30 10:59:493480browse

This article mainly shares with you an article about thinkphp5 + beanbun to achieve simple grabbing of movie URLs and pictures. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to take a look, I hope it can help everyone.

First create two data tables to store the first-level url table and the picture table under this url

dywz data table

CREATE TABLE `think_dy2018` ( 
 `id` int(7) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',  
 `movieName` varchar(255) NOT NULL COMMENT '电影名',  
 `movieUrl` varchar(520) NOT NULL COMMENT '电影详情页地址',  
 `addTime` int(11) NOT NULL COMMENT '添加时间',  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8808 DEFAULT CHARSET=utf8 COMMENT='dywz信息采集'
CREATE TABLE `think_dy2018imgs` ( 
 `id` int(8) unsigned NOT NULL AUTO_INCREMENT COMMENT '图片id',  
 `urlID` int(7) NOT NULL COMMENT '关联的电影ID',  
 `imgUrl` varchar(520) DEFAULT NULL COMMENT '图片地址',  
 `create_time` int(10) NOT NULL COMMENT '图片添加时间',  PRIMARY KEY (`id`),  
 KEY `urlID` (`urlID`)
 ) ENGINE=InnoDB AUTO_INCREMENT=1279 DEFAULT CHARSET=utf8 COMMENT='图片地址'

Next To write through thinkphp5, you must first install the extension Beanbun, and then analyze the movie website. After using Beanbun to crawl the page, use regular rules to filter out the movieName and movieUrl you want.

thinkphp5 code

1. Execute getList() to get the first-level page movieName, movieUrl
2. Execute getImage() to get the detailed information under each movie URL on the first-level page Big picture

seed = $urlList;        
  $beanbun->afterDownloadPage = function($beanbun) {           
     if (strlen($beanbun->page) < 100) {       
              $beanbun->error();           
        }       
       
       # 对抓取内容转码            
       $contents = mb_convert_encoding($beanbun->page,'utf8' ,'gb2312');            
       file_put_contents('66.html', $contents);            
       $patter = '/\s*\s*(.*)<\/a>\s*<\/b>/sU';            
       preg_match_all($patter, $contents, $m);            
       
       # 对抓取的数据分析插入数据库            
       if($m[0]){               
           $hrefs  = $m[1];                
           $titles = $m[2];                
           
           foreach ($hrefs as $key => $href){               
               $url =  Helper::formatUrl($href, $beanbun->url);                   
               $data[] = [                   
                    'movieName' => strip_tags($titles[$key]),                        
                    'movieUrl'  => $url,                        
                    'addTime'   => time()                    
               ];               
          }               
             
              Db::name('dy2018')->insertAll($data);           
        }        
    };       
             # 抓取页面之后回调        
             $beanbun->start();    
       }    
       
       /**     
          * 抓取 改url下面相信信息的imges      
          * 查询数据表中id,movieUrl。然后根据movieUrl爷们内容爬取图片(注意这里图片可能有多张,所以用循环对应同一个movieUrl的urlID)     
          *    
      */    
      public function getImage(){        
      # 返回bool        
      $result = Db::table('think_dywz')->column('id,movieUrl');        
      foreach ($result as $key => $value){       
           $result = $this->https_request($value);            
           $pattern = '//sU';            
           preg_match_all($pattern,$result,$m);           
           
          if ($m[0]) {             
             $imgs = $m[1];                
             foreach ($imgs as $k=> $v) {                  
               $data = [                      
                 'imgUrl' => $v,                       
                 'urlID'  => $key,                        
                 'create_time' => time(),                  
              ];                   
              
               Db::name('dywzimgs')->insert($data);               
            }            
         }        
     }    
  }    
  
      /**     
      * cURL万能函数     
      * @param [string] $url [请求地址]    
       * @param [arra] $data  [post的数据]     
       * @return mixed     
       */    
       private static function https_request($url, $data = null){     
          # 初始化一个cURL会话        
          $curl = curl_init();        
          
          //设置请求选项, 包括具体的url        
          curl_setopt($curl, CURLOPT_URL, $url);        
          curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);  //禁用后cURL将终止从服务端进行验证        
          curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);        
          
          if (!empty($data)){         
             curl_setopt($curl, CURLOPT_POST, 1);  //设置为post请求类型            
             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);  //设置具体的post数据      
         }        
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);        
         $response = curl_exec($curl);  //执行一个cURL会话并且获取相关回复        
         curl_close($curl);  //释放cURL句柄,关闭一个cURL会话        
         return $response;    
       }
   }

This is the complete code, I believe you are smart and have learned it.

The above is the detailed content of thinkphp5 + beanbun realizes simple crawling of movie URLs and pictures. For more information, please follow other related articles on the PHP Chinese website!