15
13

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.

Laravel5でGoutteを使う

Last updated at Posted at 2015-06-12

Goutteは有名なPHPで作られたスクレイピングツールですが、
Laravel-Goutteというものがあったので、さっそくインストールしてみました。

まずcomposer.jsonに下記を追加。

    "require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.1.*",
        "fabpot/goutte": "2.0.*" // ←これ
    },

でアップデート

composer update

使ってみよう。

laravel
use Goutte\Client;

class TestController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function getTest()
    {
        // Create Goutte Object
        $client = new Client();

        // Get Data Source
        $crawler = $client->request('GET', "http://www.yahoo.co.jp/");

        $crawler->filter('title')->each(function ($node) {
            echo $node->text() . "\n";
        });
    }

結果
スクリーンショット 2015-06-12 13.38.04.png

簡単!

15
13
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
15
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?