goutte php,Laravel 5 Facade for Goutte 一个简单的PHP Web爬虫

Laravel Facade for Goutte

This repository implements a simple ServiceProvider that makes a singleton instance of the Goutte client easily accessible via a Facade in Laravel. See @FriendsOfPHP/Goutte for more information about the PHP web scraper and its interfaces.

Installation using Composer

In your terminal application move to the root directory of your laravel project using the cd command and require the project as a dependency using composer.

$ cd ~/Sites/laravel-example-project

$ composer require weidner/goutte

This will add the following lines to your composer.json and download the project and its dependencies to your projects ./vendor directory:

// ./composer.json

{

"name": "weidner/laravel-goutte-test",

"description": "A dummy project used to test the Laravel Goutte Facade.",

// ...

"require": {

"php": ">=5.5.9",

"laravel/framework": "^5.5",

"weidner/goutte": "^1",

// ...

},

//...

}

Usage

In order to use the static interface we first have to customize the application configuration to tell the system where it can find the new service. Open the file config/app.php in the editor of your choice and add the following lines ([1], [2]):

// config/app.php

return [

// ...

'providers' => [

// ...

/*

* Package Service Providers...

*/

Weidner\Goutte\GoutteServiceProvider::class, // [1] This will register the Package in the laravel echo system

/*

* Application Service Providers...

*/

App\Providers\AppServiceProvider::class,

App\Providers\AuthServiceProvider::class,

App\Providers\EventServiceProvider::class,

App\Providers\RouteServiceProvider::class,

],

// ...

'aliases' => [

'App' => Illuminate\Support\Facades\App::class,

'Artisan' => Illuminate\Support\Facades\Artisan::class,

// ...

'Goutte' => Weidner\Goutte\GoutteFacade::class, // [2] It will register as an alias for the Goutte facade

'Hash' => Illuminate\Support\Facades\Hash::class,

// ...

],

];

Now you should be able to use the facade within your application. Laravel will autoload the corresponding classes once you use the registered alias.

// routes/web.php

Route::get('/', function() {

$crawler = Goutte::request('GET', 'https://duckduckgo.com/html/?q=Laravel');

$crawler->filter('.result__title .result__a')->each(function ($node) {

dump($node->text());

});

return view('welcome');

});

TIP: If you retrieve a "Class 'Goutte' not found"-Exception try to update the autoloader by running composer dump-autoload in your project root.

TIP: You can use the package with Lumen as well. Register the GoutteServiceProvider in bootstrap/app.php and provide the missing path to your configuration directory in your AppServiceProvider (ref #34).

Configuration

You can customize the default request options to apply to each request of the client. Copy the default configuration to your application directory first:

php artisan vendor:publish --provider="Weidner\Goutte\GoutteServiceProvider"

Open the created file in the config/goutte.php and customize the configuration options to your liking.

return [

'client' => [

'allow_redirects' => false,

'cookies' => true,

],

];

Have a look into the Guzzle Documentation for a full list of available options.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值