【问题标题】:PHP Goutte try and retryPHP Gotte 尝试并重试
【发布时间】:2017-07-19 04:45:19
【问题描述】:

我需要从网站上抓取一些数据。目标服务器部分原因,部分爬取无法成功,需要重试。代码如下:

private function fetchArchive($id) {
        $url = 'xxxx/' . $id;

        $attempt = 0;
        $base = null;
        if (Goutte::request('GET', $url)->filter('#table')->count() < 1) {
            do {
                try {
                    $base = Goutte::request('GET', $url)->filter('#table')->text();
                } catch (InvalidArgumentException $e) {
                    $attempt++;
                    sleep(2);
                    break;
                }

            } while ($attempt <= 5);
        }

事实上try($base = Goutte::request('GET', $url)-&gt;filter('#table')-&gt;text()) 不起作用,我收到了

“production.ERROR: InvalidArgumentException: 当前节点列表为空。”

我该如何解决这个问题?

【问题讨论】:

    标签: php web-crawler guzzle goutte


    【解决方案1】:

    尝试使用\InvalidArgumentException(来自根命名空间,是的)。

    还可以考虑在 HTTP 级别重试,使用 Guzzle 的中间件(如在this example 中)。更好,因为在这种情况下您可以处理与 HTTP 相关的错误。

    【讨论】:

      【解决方案2】:

      因为我用过 Laravel,所以:

      catch (\InvalidArgumentException $e) {...}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-17
        • 1970-01-01
        • 1970-01-01
        • 2014-10-06
        • 1970-01-01
        • 2013-07-31
        • 2016-05-12
        • 2011-03-22
        相关资源
        最近更新 更多