2013-08-17 25 views
0

我已經寫了一個腳本,用於網頁掃描,我從頁面獲取每個鏈接並獲取代碼中的網址,並且這個工作非常緩慢,首先需要大約50秒輸出和年齡來完成大約100個鏈接,我不明白爲什麼這個工作如此緩慢,我正在考慮緩存,但不知道這對我們有什麼幫助。
如何加快這個PHP腳本的執行

1)頁面緩存或操作碼緩存。

代碼是:

public function searchForum(){ 
     global $wpdb; 
     $sUrl = $this->getSearchUrl(); 
     $this->logToCrawler(); 
     $cid = $this->getCrawlId(); 
     $html = file_get_dom($sUrl); 

     $c=1; 
     foreach($html('div.gridBlobTitle a:first-child') as $element){ 
      $post_page = file_get_dom($element->href); 
      $post_meta = array(); 
      foreach($post_page('table#mytable img:first-child') as $img){ 
       if(isset($img->src)){ 
        $post_meta['image_main'] = self::$forumurl.$img->src;   
       } 
       else{ 
        $post_meta['image_main']=NULL; 
       } 
      } 

      foreach($post_page('table.preferences td:odd') as $elm){ 
       $post_meta[] = strip_tags($elm->getInnerText()); 
       unset($elm); 
      } 

      /*Check if can call getPlainText for description fetch*/ 

      $object = $post_page('td.collection',2); 
      $methodVariable = array($object, 'getPlainText'); 

      if(is_callable($methodVariable, true, $callable_name)){ 
       $post_meta['description'] = utf8_encode($object->getPlainText());    
      } 
      else{ 
       $post_meta['description'] = NULL; 
      } 

      $methodVariable = array($object, 'getInnerText'); 
      if(is_callable($methodVariable, true, $callable_name)){ 
       /*Get all the images we found*/ 
       $rough_html = $object->getInnerText(); 

       preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $rough_html, $matches); 
       $images = array_map('self::addUrlToItems',$matches[1]); 
       $images = json_encode($images); 
      } 

      if($post_meta[8]=='WTB: Want To Buy'){ 
       $status='buy'; 
      } 
      else{ 
       $status='sell';    
      } 
      $lastdate = strtotime(date('Y-m-d',strtotime("-1 month"))); 
      $listdate = strtotime(date('Y-m-d',strtotime($post_meta[9]))); 

      /*Check for date*/ 
      if($listdate>=$lastdate){ 

       $wpdb->query("INSERT 
            INTO tbl_scrubed_data SET 
            keywords='".esc_sql($this->getForumSettings()->search_meta)."', 
            url_to_post='".esc_sql($element->href)."', 
            description='".esc_sql($post_meta['description'])."', 
            date_captured=now(),crawl_id='".$cid."', 
            image_main='".esc_sql($post_meta['image_main'])."', 
            images='".esc_sql($images)."',brand='".esc_sql($post_meta[0])."', 
            series='".esc_sql($post_meta[1])."',model='".esc_sql($post_meta[2])."', 
            watch_condition='".esc_sql($post_meta[3])."',box='".esc_sql($post_meta[4])."', 
            papers='".esc_sql($post_meta[5])."',year='".esc_sql($post_meta[6])."',case_size='".esc_sql($post_meta[7])."',status='".esc_sql($post_meta[8])."',listed='".esc_sql($post_meta[9])."', 
            asking_price='".esc_sql($post_meta[10])."',retail_price='".esc_sql($post_meta[11])."',payment_info='".esc_sql($post_meta[12])."',forum_id='".$this->getForumSettings()->ID."'");  

       unset($element,$post_page,$images); 
      } /*END: Check for date*/ 

     } 
     $c++; 
    } 

注:
1)我使用[加農DOM解析器用於解析HTML [1]。 [1]:https://code.google.com/p/ganon/wiki/AccesElements
2)在帶有WAMP,Mysql 5.5 PHP 5.3和1 GB RAM的Windows XP上。
如果您需要更多信息,請評論他們。

謝謝

+0

你有沒有考慮過使用更快的解析器? –

+0

@ ignacio-vazquez-abrams:/我從來沒有聽說過這種事情在PHP中,你是說PHP快速HTML解析器? – ravisoni

+0

我在說不同的解析器。哪個可能運行得更快。 –

回答

2

你需要弄清楚你的程序的哪些部分是緩慢的。有兩種方法可以做到這一點。

1)加入一些打印報表,打印出各個地方的時間,所以你可以說:「嘿,看,這需要5秒鐘,從這裏到這裏。」

2)使用像xdebug這樣的分析器,它可以運行你的程序並在運行時進行分析,然後你就可以知道哪部分代碼是慢的。

只看一個程序,你不能說「哦,這是加速的一個緩慢的部分。」不知道什麼是緩慢的,你可能會浪費時間來加速那些不是緩慢部分的部分。

+0

+1感謝xdebug。 – ravisoni