こんばんは、のるぼるのんです。
前回に引き続き、ウェブスクレイピングを一から勉強していきたいと思います。
今回は、Goutteを導入し、このブログのトップページの情報を抜き出してみたいと思います。頑張りましょう。
(なんて読むんだろ、、、)
1.Goutteをcomposerでプロジェクトに導入、app.phpに記述。
composerで導入します。
1 |
composer require weidner/goutte |
ver 1.0が導入されました。
app.phpに記述を追加し、使えるようにします。
・app.php
1 2 3 4 5 6 7 |
'providers' => [ Weidner\Goutte\GoutteServiceProvider::class, // 追記 ], 'aliases' => [ 'Goutte' => Weidner\Goutte\GoutteFacade::class, // 追記 ]; |
さあて、Scraping.phpを進化させましょう。
2.Scraping.phpに記述
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Goutte; // app.phpに追記したので、aliasが使えます。 class Scraping extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'Scraping'; /** * The console command description. * * @var string */ protected $description = 'ウェブスクレイピングを実行します。'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { echo 'スクリプトを実行します。' . PHP_EOL; $html = Goutte::request('GET', 'https://tac-blog.tech'); // トップページの情報を取得 dump($html); // dumpで表示してみます。 echo 'スクリプトを終了します。' . PHP_EOL; } } |
実行結果は、、、
ふむふむ
オロロロロロロrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
情報の取得は出来ましたが、こんなんじゃ目的を達成したとは到底言えません!
Goutteのフィルタリングを理解する必要がありそうですね。
次回はGoutteについて、色々やって見ていこうと思います。よろしくお願いします!
あ、ちなみにGoutteは「グート」と読むらしいです。
コメントを書く