【问题标题】:laravel goutte node list is emptylaravel gotte 节点列表为空
【发布时间】:2017-11-18 04:48:11
【问题描述】:

我遇到了 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.

第一个没有图片的新闻出现当前节点列表为空,请问如何解决?

【问题讨论】:

  • 如果您想跳过它,请将其包装在 try catch 块中。如果那是新画廊的尽头,您可以指示刮板导航到其他地方。
  • 任何代码示例?

标签: laravel laravel-5 goutte


【解决方案1】:
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?
}

【讨论】:

  • 那么你的异常来自其他地方。尝试使用 try catch 包装更多代码并查看失败的地方。
猜你喜欢
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-14
  • 2017-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多