2012-04-14 44 views
8

我想創建一個從0到10 000 000逐個獲取所有頁面信息的抓取工具。我無所謂需要多少時間。我只是想它的工作。以下是錯誤我獲得達到'100'的最大函數嵌套級別,正在中止

Fatal error: Maximum function nesting level of '100' reached, aborting! in D:\wamp\www\crawler\index.php on line 25

線25

$htmlstr = (string)$this->curlGet($url); 

而且有我完整的腳本。

謝謝你的幫助!

header('Content-Type: text/html; charset=utf-8'); 
ini_set('max_input_nesting_level','100000'); 
ini_set('max_execution_time','100000'); 

class crawler{ 

    private $url; 
    private $page; 
    private $bothurl; 
    private $innerDom = null; 
    public $prop; 
    public $entry; 

    function __construct($entry){ 
     $this->entry = $entry; 
     $this->bothurl = array('http://www.remax-quebec.com/fr/inscription/Q/'.$entry.'.rmx','http://www.remax-quebec.com/en/inscription/Q/'.$entry.'.rmx'); 
     $this->scan(); 
    } 

    private function scan(){ 
     $i =0; 
     foreach($this->bothurl as $url){ 
      $this->url = $url; 
      $this->lang = ($i==0)?'fr':'en'; 
      $htmlstr = (string)$this->curlGet($url); 
      $dom = new DOMDocument; 
      @$dom->loadHTML($htmlstr); 
      $this->page = $dom; 
      $this->htmlInfos(); 
      $this->getInfos(); 
      $i++; 
     } 
    } 

    private function htmlInfos(){ 
     $divs = $this->page->getElementsByTagName('div'); 
     foreach($divs as $div){ 
      if($div->hasAttribute('class') && $div->getAttribute('class') == 'bloc specs'){ 
       $innerDom = new DOMDocument(); 
       @$innerDom->loadHTML($this->innerHTML($div)); 
       $this->innerDom = $innerDom; 
      } 
     } 
     if($this->innerDom === null) $this->changeEntry(); 
    } 

    private function getInfos(){ 
     $sect = 0; 

     foreach($this->innerDom->getElementsByTagName('div') as $div){ 
     # obtenir la description 
      $this->getDesc($div->getAttribute('class'),$div); 
     # obtenir les caractéristiques 
      $this->getCaract($div->getAttribute('class'),$div); 
     # obtenir les informations interieur, exterieur et evaluation 
      if($div->getAttribute('class') == 'section deux-colonnes'){ 
       switch($sect){ 
        case 0: $this->getSpecInfos($div,'interieur'); break; 
        case 1: $this->getSpecInfos($div,'exterieur'); break; 
        case 2: $this->getSpecInfos($div,'evaluation'); break; 
        case 3: $this->getSpecInfos($div,'equipement'); break; 
        case 4: $this->getSpecInfos($div,'services'); break; 
       } 
       $sect++; 
      }else if($div->getAttribute('class') == 'section'){ 
     # obtenir les détails des pièces 
       foreach($div->getElementsByTagName('table') as $table){ 
        if($table->getAttribute('class') == 'details-pieces'){ 
         $this->detailPieces($table); 
        } 
       } 
      } 
     } 
    } 

    private function getDesc($class,$obj){ 
     if($class == 'section description'){ 
      $p = $obj->getElementsByTagName('p')->item(0); 
      $text = (string)$p->nodeValue; 
      $this->prop[$this->lang]['description'] = $text; 
     } 
    } 

    private function getCaract($class,$obj){ 
     if($class == 'section characteristiques'){ 
      foreach($obj->getElementsByTagName('div') as $div){ 
       if(substr($div->getAttribute('class'),0,4) == "item"){ 
        $text = (string)$div->nodeValue; 
        $this->prop[$this->lang]['caracteritiques'][substr($div->getAttribute('class'),5)] = $text; 
       } 
      } 
     } 
    } 

    private function getSpecInfos($obj,$nomInfo){ 
     foreach($obj->getElementsByTagName('table') as $table){ 
      foreach($table->getElementsByTagName('tr') as $tr){ 
       $name = $tr->getElementsByTagName('td')->item(0); 
       $value = $tr->getElementsByTagName('td')->item(1); 
       $name = substr((string)$name->nodeValue,0,-2); 
       $value = (string)$value->nodeValue; 
       $this->prop[$this->lang][$nomInfo][$this->noAccents($name)] = $value; 
      } 
     } 
    } 

    private function detailPieces($obj){ 
     $tbody = $obj->getElementsByTagName('tbody')->item(0); 
     foreach($tbody->getElementsByTagName('tr') as $tr){ 
      $name = $tr->getElementsByTagName('td')->item(0); 
      $name = (string)$name->nodeValue; 
      $level = $tr->getElementsByTagName('td')->item(1); 
      $level = (string)$level->nodeValue; 
      $dimensions = $tr->getElementsByTagName('td')->item(2); 
      $dimensions = (string)$dimensions->nodeValue; 
      $floor = $tr->getElementsByTagName('td')->item(3); 
      $floor = (string)$floor->nodeValue; 
      $desc = $tr->getElementsByTagName('td')->item(4); 
      $desc = (string)$desc->nodeValue; 

      $this->prop[$this->lang]['pieces'][$this->noAccents($name)]['etage'] = $level; 
      $this->prop[$this->lang]['pieces'][$this->noAccents($name)]['dimensions'] = $dimensions; 
      $this->prop[$this->lang]['pieces'][$this->noAccents($name)]['revetement'] = $floor; 
      $this->prop[$this->lang]['pieces'][$this->noAccents($name)]['description'] = $desc; 
     } 
    } 

    private function innerHTML($element){ 
     $innerHTML = ""; 
     $children = $element->childNodes; 
     foreach ($children as $child) 
     { 
      $tmp_dom = new DOMDocument(); 
      $tmp_dom->appendChild($tmp_dom->importNode($child, true)); 
      $innerHTML.=trim($tmp_dom->saveHTML()); 
     } 
     return $innerHTML; 
    } 

    private function noAccents($value){ 
     $string= strtr($chaine,"ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ","aaaaaaaaaaaaooooooooooooeeeeeeeecciiiiiiiiuuuuuuuuynn"); 
    } 

    private function changeEntry(){ 
     $this->entry++; 
     echo $this->entry; 
     $this->scan(); 
    } 

    private function curlGet($url){ 
     $curl = curl_init(); 
     curl_setopt($curl, CURLOPT_URL, $url); 
     curl_setopt($curl, CURLOPT_ENCODING, "gzip"); 
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 
     $data = curl_exec($curl); 
     curl_close($curl); 
     return $data; 
    } 
} 

$entry = 8678057; 

$crawler = new crawler($entry); 

echo '<pre>'; 
print_r($crawler->prop); 
echo '</pre>'; 
+2

您是否在複製Google? – 2012-04-14 21:50:46

+3

我真的不知道在這裏努力去理解這個問題。 – simchona 2012-04-14 21:51:28

+3

你有錯誤。這很好。你已經發布了代碼。這也很好,但你的問題在哪裏?你想讓我們神奇地修復你的代碼並在這裏發佈一個工作解決方案嗎? – Bojangles 2012-04-14 21:51:45

回答

33

假設你使用了XDebug,您可以用

ini_set('xdebug.max_nesting_level', $limit) 
+0

使用資產與laravel 4中的less.php插件來編譯bootstrap,我達到100最大錯誤,增加到200允許它運行。似乎是合法的。 – 2014-02-21 23:42:23

2

/etc/mysql/my.cnf參數文件更改爲類似的東西 max_allowed_pa​​cket個= 512M

設置自己的極限

確保你已經安裝了xdebug(使用phpinfo()),然後更改文件/etc/php5/fpm/php.ini添加或編輯該行: xdebug.max_nesting_level = 1000

重新啓動這兩個服務 sudo的服務mysql的重啓 須藤服務PHP5-FPM重啓

如果你仍然可以在/etc/php5/fpm/php.ini Xdebug的設置這些兩個參數爲false這是行不通的。 remote_autostart = 0 xdebug.remote_enable = 0

1

就我而言,它與作曲家有關。某些供應商在composer.json文件中進行了更新,但我忘記了運行命令作曲家更新作曲家安裝。系統產生了一連串errros,這導致了這個'最大嵌套級別'。

執行這些命令後,問題得到了解決

0

假設你沒有做一個下拉死機錯誤,只是改變了XDebug的限制。

我解決了這個問題,通過更改xdebug.ini文件。 (在我的Mac上,路徑是/usr/local/php5-5.6.17-20160108-103504/php.d/50-extension-xdebug.ini,也許你會有點不同。)

在xdebug.ini文件的底部添加新行:

xdebug.max_nesting_level = 500


記住:您必須更改與您使用的是PHP的xdebug.ini。例如,如果您在計算機中安裝了php5和xampp,則需要確定您正在使用哪個php。

相關問題