从特定div中获取内部文本

时间:2012-03-04 15:38:06

标签: php curl fetch

我无法从网页中获取内容, 实际上我想从div名称displaybody

中获取所有内部文本

但是我的代码似乎不起作用,它正在获取页面的所有内容而不是从displaybody div获取内容,并且在4页成功获取后我收到错误,

  

致命错误:第127行的E:\ Installations \ xampp \ htdocs \ wp \ simple_html_dom.php超出了30秒的最长执行时间

以下是脚本的代码,

我希望我的脚本打开代码中提到的url中的所有子页面(/ txt /任意数字)并从其特定的div(displaybody)中获取内容

<?php

    $request_url ='http://www.zedge.net/txts/4519/200-3-1/';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $request_url);    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);

    $regex='/href=\"\/txt\/[0-9].*/';
    preg_match_all($regex,$result,$parts);

    foreach($parts[0] as $link){
        $url = 'http://zedge.net' . str_replace ("href=\"",'',$link);
        echo file_get_html($url)->plaintext; 
        echo "<br /><br /   ><br />";
    }

    curl_close($ch);    
    echo $html->find('displaybody', 0)->innertext;      
?>

2 个答案:

答案 0 :(得分:2)

错误意味着您的脚本执行时间过长,因此会关闭。如果获取页面只需要很长时间,这没有问题,您可以禁用或增加最大值。 set_time_limit()的执行时间。也有可能是一个错误导致你的脚本在一个部件上停留太长时间,如果你怀疑是这种情况,你应该测量脚本不同部分的时间,看看是什么导致脚本挂起。

至于您的其他问题,您想从每个页面获取div#displaybody内容吗?假设URL提取已经有效,我想你可以在foreach循环中执行此操作:

$html = file_get_html($url);
$displaybody = $html->find('div[id=displaybody]', 0)->plaintext;

答案 1 :(得分:-1)

您可以通过

将max_execution_time设置为0
ini_set('max_execution_time', 0);

这将删除时间限制。