我正在寻找一种很好的方法来做到这一点:我目前的方法似乎不允许搜索深度超过 30-40,即使在编辑php.ini
设置后也希望增加默认执行时间以及最大内存使用量。基本上,一旦搜索深度超过此量,服务器就会崩溃。
这是我的代码(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']);
HTML标签的数量应该很容易获得
$this->_dom->getElementsByTagName("*")->length;
如下: 计算页面 PHP 中的所有 HTML 标记
$dom = new DOMDocument;
$dom->loadHTML($HTML);
$allElements = $dom->getElementsByTagName('*');
echo $allElements->length;
尽管链接中的示例没有获得接近您拥有的嵌套级别数的事件......