0

これを行う良い方法を探しています: 私の現在の方法では、php.iniデフォルトの実行時間と最大メモリ使用量を増やすことを期待して設定を編集した後でも、30-40 を超える深さの検索は許可されていないようです。基本的に、検索の深さがこの量を超えるとすぐに、サーバーがクラッシュします。

これが私のコードです(private function _ParseHtml($html, $depth = nDepth):

        if ($depth === 0)
        {
            return;
        }

        @$this->_dom->loadHTML($html);

        $this->nodes = $this->_dom->childNodes;

        $html = array();
        $iterCount = 0;

        foreach($this->nodes as $node)
        {
            if($node->hasChildNodes())
            {
                $html[$iterCount++] = $node->C14N();    
            }

            $this->_tagCount++;

            if ( $this->_config['Debug'] ) _wrapBreak("Tag Count incremented");
        }

        if( count( $html ) > 0 )
        {
            $static_depth = $depth - 1;

            foreach( $html as $parse )
            {
                $this->_ParseHtml( $parse, $static_depth );

                if ( $this->_config['Debug'] ) _wrapBreak("ParseHtml did return");
            }
        }

        _wrapBreak("<strong>Current Depth</strong> => <strong>{$depth}</strong>");

_Invoke()スクレイプ機能のメインコードと同様に:

             $handle = curl_init($this->_url);

         curl_setopt($handle, CURLOPT_BUFFERSIZE, self::BUFSIZE); //BUFSIZE == 50000
         curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);

         $this->_data['html'] = curl_exec($handle);

         curl_close($handle);

     $this->_ParseHtml($this->_data['html']);
4

2 に答える 2

1

HTMLタグの数は簡単に取得できるはずですが、

$this->_dom->getElementsByTagName("*")->length;
于 2012-08-13T17:47:50.250 に答える
1

ここにあるように:ページPHPのすべてのHTMLタグを数えます

$dom = new DOMDocument;
$dom->loadHTML($HTML);
$allElements = $dom->getElementsByTagName('*');
echo $allElements->length;

リンクの例では、ネストされたレベルの数に近いイベントは発生しませんが...

于 2012-08-13T17:48:03.823 に答える