【问题标题】:Server crashes when scraping using cURL & simple_html_dom使用 cURL 和 simple_html_dom 抓取时服务器崩溃
【发布时间】:2018-07-26 15:12:06
【问题描述】:

我遇到了一个奇怪的问题。我有一个在 localhost 上运行良好的脚本,但是在服务器上运行它时,它在几个循环后崩溃。该脚本使用cURLsimple_html_dom 来抓取网页。

这是代码的总和:

    class updateController extends Controller{
        function __construct(){
            ini_set('max_execution_time', 0);
            set_time_limit(0);
            require_once 'simple_html_dom.php';
        }
static public function ThemeforestLoopExisting(){
   $themes = Fulls::where('X','Y')->get();

   foreach($themes as $theme){
       $cURL = GeneralFunctions::cURLDom($theme['url']);
     //Here I search for specific parts on the web page using the "find" method on simple_html_dom
   }
}



  }

GeneralFunctions.php:

    static public function cURL_scraping($url){
        $curl = curl_init();
         curl_setopt($curl, CURLOPT_URL, $url);
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
         curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
         curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
         curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A');
         curl_setopt($curl,CURLOPT_HTTPHEADER,array('Expect:'));
         curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
         curl_setopt($curl, CURLOPT_ENCODING, 'identity');
        $response['str'] = curl_exec($curl);


        $response['header'] = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        curl_close($curl);
        return $response;
    }

    static public function cURLDom($url){

  $cURL_results   = generalFunctions::cURL_scraping($url);
  $res['header']  = $cURL_results['header'];
  $res['str']  = str_get_html($cURL_results['str'],$lowercase=false, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=false, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT);
  return $res['str'];
}

整个过程在前 10/20/30 次运行时都有效,然后服务器崩溃了。它在本地主机上完美运行。 我和我的虚拟主机谈过了,但他们没有帮助。

这里有什么我遗漏或没有意识到的吗? 任何帮助将非常感激... 谢谢!

【问题讨论】:

  • 服务器崩溃了?还是脚本?
  • 因什么错误而崩溃? max_execution_time 或许?
  • 服务器崩溃 - 我收到 500 错误消息:内部服务器错误 服务器遇到内部错误或配置错误,无法完成您的请求。请联系服务器管理员 webmaster@xxxxxxx.com 并告知他们错误发生的时间,以及您可能所做的任何可能导致错误的事情。服务器错误日志中可能提供有关此错误的更多信息。此外,在尝试使用 ErrorDocument 处理请求时遇到 500 Internal Server Error 错误。
  • 好吧,这不是服务器崩溃。那是脚本失败并且 Web 服务返回错误代码。 Web 服务器日志文件应包含问题的详细信息。
  • 我在日志中找不到任何有用的东西,甚至网络主机也不知道。顺便说一句 - 当脚本编写不正确或我通常会得到 Laravel 错误时,在这种情况下我不会得到同样类型的错误

标签: php curl screen-scraping simple-html-dom


【解决方案1】:

这实际上是一个数据库问题。我将排序规则更改为 utf8mb4_general_ci 并修复了它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多