2017-06-15 8 views
0

goutteに問題があります。一部のコンテンツには画像がありますが、画像がない場合は無視して画像を保存せずに続行してくださいそれがあれば、それを保存してください。私は出力を持っている現時点ではlaravel 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. 

現在のノードリストが空である、これは解決することができるか、イメージを持っていない最初のニュースで起こりますか?

+0

過去に移動したい場合は、try catchブロックで囲みます。新しいギャラリーの終わりがあれば、他の場所をナビゲートするようにスクレーパーに指示することができます。 – btl

+0

どのようなコード例ですか? – Przemek

答えて

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? 
} 
+0

はまだ私にエラーを投げます – Przemek

+0

あなたの例外は他のどこかから来ています。 try catchでより多くのコードをラップし、どこが失敗したかを見てください。 – btl

関連する問題