使用php库simple_html_dom,我循环遍历url列表,作为dom,对于每个url,我尝试找到一个字符串,如果找到,我将url保存在一个数组中,否则我进入下一个循环,在最后返回url数组。对于每个url,该脚本大约需要几秒的时间。在一些循环之后,脚本被卡在文件get html内的$dom->load($url)行,抛出了一个分段错误,循环的数量在不同的$url列表上是不同的。我试图在一个测试脚本中隔离加载时的调用($url),该脚本只在循环脚本被卡住的url上工作,但测试脚本结束时没有错误(但我无法检查dom的print_r,因为如果我试图查看页面源代码,我的firefox就会崩溃)。我在LAMP服务器上工作。代码如下:
error_reporting(E_ALL);
ini_set("max_execution_time", "300");
ini_set("memory_limit", "512M");
ini_set('output_buffering', 0);
ini_set('implicit_flush', 1);
ob_end_flush();
ob_start();
set_time_limit(100);
$urlArray = array();
foreach($urlArray as $url){
$found = false;
$dom = file_get_html($url);
foreach(( $dom->find('target')) as $caught){
array_push($link, $caught);
$found = true
}
if($trovato){
return $link;
}else{
echo "not found";
}
}thx寻求任何帮助
发布于 2013-02-20 04:27:48
这是一个常见的问题,这里有一个bug http://sourceforge.net/p/simplehtmldom/bugs/103/。在您的if语句之前添加以下行:
$dom->clear();
unset($dom);大多数情况下,在此之后您将看不到任何段错误。但是如果你解析了几千个urls (像我这样:),你可能会再次遇到它。所以我的解决方案是-打开simple_html_dom.php文件,注释146到149之间的所有行。
function clear()
{
/*
$this->dom = null;
$this->nodes = null;
$this->parent = null;
$this->children = null;
*/
}更新:如果您注释此行-您的内存消耗将增加每次解析迭代
https://stackoverflow.com/questions/12729446
复制相似问题