我正在寻找一种很好的方法来做到这一点:我目前的方法似乎不允许超过30-40的搜索深度,即使编辑了php.ini设置后希望增加默认执行时间以及最大内存使用量。基本上,只要搜索深度超过这个数量,服务器就会崩溃。获取页面html标签数量的好方法
这里是我的代码(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("Current Depth => {$depth}");
以及针对刮_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']);