最近速攻php后台思想,分享一個用php實現一個簡單的爬蟲,抓取電影網站的視頻下載地址


昨天沒什么事,先看一下電影,就用php寫了一個爬蟲在視頻網站上進行視頻下載地址的抓取,半個多小時,大約抓取了3萬多條ftp地址數據,效果還是可以的。這里總結一下抓取過程中遇到的問題

1:通過訪問瀏覽器來執行php腳本這種訪問方式其實並不適合用來爬網頁,因為要受到php的連接時間,內存等的限制,當然了這里是可以修改php.ini的配置文件,但是還是不推薦使用,

php做長周期的任務的時候推薦使用php-cli(命令行)的方式進行,這樣效率相比web訪問的效率要高得多的多

2:在爬取頁面的時候,一開始使用的是file_get_contents()的方式,結果執行一段時間以后php就會報錯(failed to open stream: No error ),后來查了一下,有人說curl()函數不會出現上述問題,就采用了curl的方式去采集數據,沒有問題

3:在進行采集之前,最好修改一下php.ini中的參數 user_agent(具體請百度)

下面是抓取的代碼:

<?php
header( "Content-type:text/html;Charset=utf-8" );
class Get_info{
     //沒有爬過的url
     private $url_arr_no = [ ];
     //已經爬過的url
     private $url_arr_over = [ ];
     //獲取url的正則表達式
     private $url_reg ="/<a href=['\"](.*?)['\"].*?>(.*?)<\/a>/i";
     //獲取ftp地址的正則表達式
     private $ftp_reg = "/<td[\d\D]*?><a href=\"([\d\D]*?)\">[\d\D]*?<\/a><\/td>/i";
     //url前綴
     private $prefix_url = null;
     //查找到的數據
     public $ftp_result = [ ];
     public function __construct( $url = "" ){
            if( empty( $url ) ){
                    echo "url不能為空";
                    return false;
            }
            $this ->url_arr_no[ ] = $url;
            $this ->prefix_url = $url;
     }
     //開始執行
     public function start( ){
         echo "查找開始<br/>";
             $ch = curl_init( );
             curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 );
             curl_setopt ( $ch , CURLOPT_CONNECTTIMEOUT ,10 );
         while( ! empty( $this ->url_arr_no ) ){
               //foreach ( $this->url_arr_no as $key => $value ) {  
                      $value = array_shift( $this->url_arr_no );
                          if( substr( $value, 0,8 )  == "/webPlay"){
                                      continue;
                          }
                 if( ! in_array( $value , $this->url_arr_over ) ){ //如果需要查找的url沒有爬過,就開始爬
                                     curl_setopt ($ch, CURLOPT_URL, $value );
                                     $content = curl_exec($ch);
                                      //利用正則進行解析頁面內容
                                      preg_match_all( $this->url_reg, $content , $url_match );
                                      preg_match_all( $this->ftp_reg, $content , $ftp_match );
                             //如果新查到的url已經在待查詢或者已經查詢的數組中存在,就不添加
                             if( ! empty( $url_match[1] ) ){
                                       foreach( $url_match[1] as $url ){
                                                   if( ! in_array( $url, $this->url_arr_no ) && ! in_array( $url,$this->url_arr_over )){
                                                          $this ->url_arr_no[ ] = $this ->prefix_url.$url;
                                          }
                                       }
                             }
                              //如果ftp地址已經存在,就不進行存儲
                              if( ! empty( $ftp_match[1] ) ){
                                    foreach( $ftp_match[1] as $ftp ){
                                          if( ! in_array( $ftp, $this->ftp_result ) ){
                                                 $this ->ftp_result[ ] = $ftp;
                                                 file_put_contents("result.txt" , $ftp."\r\n" , FILE_APPEND);
                                          }
                                    }
                              }
                             $this ->url_arr_over[ ] = $value;
                             $key_arr = array_keys( $this->url_arr_no,$value );
                             if( ! empty( $key_arr ) ){
                                      foreach( $key_arr as $k => $v ){
                                          unset( $this->url_arr_no[ $v ] );
                                      }
                             }
                 }
          //}
         }
             echo "查找完畢";
     }
}
$url = "";
$class = new Get_info( $url );
$class -> start( );
 ?>

出處:http://gold.xitu.io/post/582acfeba0bb9f006794ccc3


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM