【问题标题】:InvalidArgumentException in Crawler.php line 516: The current node list is empty. with laravel-goutteCrawler.php 第 516 行中的 InvalidArgumentException:当前节点列表为空。与 laravel-goutte
【发布时间】:2017-06-02 13:16:18
【问题描述】:

为什么当我执行 laravel-goutte 文档中的代码时它不起作用,该代码在 github 上的包的主页中:

https://github.com/dweidner/laravel-goutte

use Weidner\Goutte\GoutteFacadeGoutte;


Route::get('/', function() {
    $crawler = Goutte::request('GET', 'http://duckduckgo.com/?q=Laravel');
    $url = $crawler->filter('.result__title > a')->first()->attr('href');
    dump($url);
    return view('welcome');
});

并显示错误

我使用 laravel 2.2.29

【问题讨论】:

标签: php laravel web-scraping laravel-5.2 goutte


【解决方案1】:

您的filter没有返回任何结果。这就是它崩溃的原因。这就是我通过添加 try catch 解决此问题的方法。

try {
   $url = $crawler->filter('.result__title > a')->first()->attr('href');
} catch (\InvalidArgumentException $e) {
    // Handle the current node list is empty..
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2017-11-18
    • 2020-04-29
    • 1970-01-01
    相关资源
    最近更新 更多