后来我终于知道 , 它并不是我的花 ,我只是恰好途径了它的盛放。
### 准备
项目开始前, 我们需要安装一些必要的依赖包
1 2 3
| composer require guzzlehttp/guzzle composer require fabpot/goutte composer require symfony/dom-crawler
|
并创建一个命令
1
| php artisan make:command Spider
|
代码
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
| <?php
namespace App\Console\Commands;
use Goutte\Client; use Illuminate\Console\Command; use Symfony\Component\DomCrawler\Crawler;
class Spider extends Command { protected $signature = 'run:dev';
protected $description = 'Command description';
protected $startUrl = 'https://laracasts.com/series/learn-vue-2-step-by-step'; protected $client; public function __construct() { parent::__construct(); $this->client = new Client(); } public function handle() { $crawler = $this->client->request('GET', $this->startUrl); $crawler->filter('.episode-list-title > a')->each(function ($node) use ($crawler) { $hrefText = trim($node->text()); $link = $crawler->selectLink($hrefText)->link(); $craw = $this->client->click($link); $domElement = new Crawler($craw->html()); $videoSrc = $domElement->filter('video > source')->first()->attr('src'); $this->line($videoSrc); }); $this->line('success'); } }
|
https://laracasts.com/series/learn-vue-2-step-by-step
https://symfony.com/doc/current/components/dom_crawler.html
https://packagist.org/packages/fabpot/goutte