4
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Goutte を使ってスクレイピングをしてみた

4
Posted at

Goutte を使って、Yahooニュースの情報を取得してみました。
とても、簡単で便利ですね。

動作環境

  • Laravel5.4
  • PHP5.6
  • Sqlite
  • MacOS10.12

インストール

composer require weidner/goutte

初期設定

config/app.php

providers 配列に以下を追加

Weidner\Goutte\GoutteServiceProvider::class,

aliases 配列に以下を追加

'Goutte' => Weidner\Goutte\GoutteFacade::class,

スクレイピング設定

Yahoo ニュースからタイトル、URL、サムネイル画像を取得する
※サムネイル画像は、存在しない場合があるので、存在チェックをしている

Route::get('/goutte', function() {
    $crawler = Goutte::request('GET', 'https://news.yahoo.co.jp/list/?c=economy');
    $crawler->filter('.listArea .list li')->each(function ($node) {
      $val['url'] = $node->filter('a')->attr("href");
      $val['title'] = $node->filter('.ttl')->text();
      if ($node->filter('.imgWrap .image img')->count() > 0) {
        $val['thumbnail'] = $node->filter('.imgWrap .image img')->attr('data-src');
      }
      dump($val);
    });
    return;
});

こんな感じで取得できました。

スクリーンショット 2017-06-25 22.33.23.png

4
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?