我正在寻找一种好方法:我现在的方法似乎不允许搜索深度超过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>");
以及scrape _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']);
答案 0 :(得分:1)
尽管
,但很容易获得HTML标签的数量$this->_dom->getElementsByTagName("*")->length;
答案 1 :(得分:1)
在此处找到:Count all HTML tags in page PHP
$dom = new DOMDocument;
$dom->loadHTML($HTML);
$allElements = $dom->getElementsByTagName('*');
echo $allElements->length;
虽然链接中的示例没有使事件接近您拥有的嵌套级别数...