laravel goutte节点列表为空

时间:2017-06-15 15:45:38

标签: laravel laravel-5 goutte

我有一个关于goutte的问题,有些内容有图像而有些内容没有,如果没有任何图像,我希望它忽略它并继续保存而不保存图像但如果除此之外,还有。

$crawler->filter('div[id=content]>.teaser-50')->each(function ($node, $key) {
        $title = $node->filter('.plain')->text();
        $datepublished = $node->filter('.dateonline')->text();
        $description = $node->filter('.teaser-link')->text();
        $link = $node->filter('a')->link();
        if(!empty ($link_r = $link->getUri())) {
        $image = $node->filter('img')->image();
        $image_s = $image->getUri();
        $filename = basename($image_s);
        $image_path = ('news-gallery/' . $filename);
        Image::make($image_s)->save(public_path('news-gallery/' . $filename));
        }
        $id = 1+ $key + 1;
        $news = News::where('id', $id)->first();
        // if news is null
        if (!$news) {
            $news = new News();
        }
        $news->title = $title;
        $news->datepublished = $datepublished;
        $news->description = $description;
        $news->link = $link_r;
        $news->image = $image_path;
        $news->save();
        $this->info('Scraping done succesfully');
    });

目前我有输出:

λ php artisan scrape:news
Scraping done succesfully
Scraping done succesfully
Scraping done succesfully
Scraping done succesfully


  [InvalidArgumentException]
  The current node list is empty.

当前节点列表在第一个没有图像的新闻上出现空白,如何解决?

1 个答案:

答案 0 :(得分:0)

try {
        $title = $node->filter('.plain')->text();
        $datepublished = $node->filter('.dateonline')->text();
        $description = $node->filter('.teaser-link')->text();
        $link = $node->filter('a')->link();
        if(!empty ($link_r = $link->getUri())) {
        $image = $node->filter('img')->image();
        $image_s = $image->getUri();
        $filename = basename($image_s);
        $image_path = ('news-gallery/' . $filename);
        Image::make($image_s)->save(public_path('news-gallery/' . $filename));
        }
        $id = 1+ $key + 1;
        $news = News::where('id', $id)->first();
        // if news is null
        if (!$news) {
            $news = new News();
        }
        $news->title = $title;
        $news->datepublished = $datepublished;
        $news->description = $description;
        $news->link = $link_r;
        $news->image = $image_path;
        $news->save();
        $this->info('Scraping done succesfully');
} catch (InvalidArgumentException $e) {
        // do something else when the error occurs...
        // go to a different page?
        // scrape different elements?
}