可以透過以下幾個步驟,並搭配相關套件來達成:
1. 安裝 Goutte 套件:
Goutte 是一個基於 Symfony DomCrawler 和 BrowserKit 的套件,它提供了一個簡單的 API 來抓取網站內容。
composer require fabpot/goutte
2. 建立一個 Laravel Command:
使用 Laravel 的 Artisan 指令來建立一個 command,方便我們執行爬蟲任務。
php artisan make:command CrawlWebsite
3. 在 Command 中撰寫爬蟲邏輯:
打開 app/Console/Commands/CrawlWebsite.php
,並在 handle()
方法中撰寫爬蟲邏輯。
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Goutte\Client;
use Symfony\Component\DomCrawler\Crawler;
class CrawlWebsite extends Command
{
protected $signature = 'crawl:website {url}';
protected $description = 'Crawl a website and extract data';
public function handle()
{
$url = $this->argument('url');
$client = new Client();
$crawler = $client->request('GET', $url);
// 範例:抓取所有 h2 標題
$crawler->filter('h2')->each(function (Crawler $node) {
$this->info($node->text());
});
// 在這裡加入您自己的抓取邏輯
// ...
}
}
4. 執行爬蟲 Command:
使用 Artisan 指令執行您的爬蟲 command,並傳入要抓取的網址。
php artisan crawl:website https://example.com
5. 進階應用:
- 資料儲存:
- 將抓取到的資料儲存到資料庫或檔案中。
- 可以使用 Laravel 的 Eloquent ORM 或檔案系統相關功能。
- 分頁處理:
- 如果目標網站有分頁,可以使用 Goutte 的
link()
方法來抓取下一頁的連結。
- 如果目標網站有分頁,可以使用 Goutte 的
- 異常處理:
- 加入異常處理機制,例如處理網路錯誤、網站結構變更等。
- 設定爬蟲頻率:
- 使用Laravel的task scheduling來設定爬蟲執行的頻率。
範例:
以下提供一個更詳細的範例,抓取某個網站的產品名稱和價格:
// ... (在 handle() 方法中)
$crawler->filter('.product-item')->each(function (Crawler $node) {
$productName = $node->filter('.product-name')->text();
$productPrice = $node->filter('.product-price')->text();
$this->info("Product: $productName, Price: $productPrice");
// 將資料儲存到資料庫
// DB::table('products')->insert([
// 'name' => $productName,
// 'price' => $productPrice,
// ]);
});
// ...
透過以上步驟,您就可以快速地使用 Laravel 10 建立一個爬蟲功能。
將 Laravel 爬蟲功能轉換為 API,可以讓其他應用程式或服務透過網路請求來使用您的爬蟲功能。以下是如何將爬蟲功能改為 API 的步驟:
1. 建立一個 Laravel Controller:
使用 Artisan 指令建立一個 controller,用於處理 API 請求。
php artisan make:controller CrawlApiController
2. 在 Controller 中撰寫 API 邏輯:
打開 app/Http/Controllers/CrawlApiController.php
,並在 controller 中撰寫 API 邏輯。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Goutte\Client;
use Symfony\Component\DomCrawler\Crawler;
class CrawlApiController extends Controller
{
public function crawl(Request $request)
{
$url = $request->input('url');
if (!$url) {
return response()->json(['error' => 'URL is required'], 400);
}
$client = new Client();
$crawler = $client->request('GET', $url);
$data = [];
$crawler->filter('.product-item')->each(function (Crawler $node) use (&$data) {
$productName = $node->filter('.product-name')->text();
$productPrice = $node->filter('.product-price')->text();
$data[] = [
'name' => $productName,
'price' => $productPrice,
];
});
return response()->json($data);
}
}
3. 定義 API 路由:
在 routes/api.php
中定義 API 路由,將請求導向到 controller 的 crawl()
方法。
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CrawlApiController;
Route::post('/crawl', [CrawlApiController::class, 'crawl']);
4. 測試 API:
您可以使用 Postman 或其他 API 測試工具來測試您的 API。
- 請求方式: POST
- URL:
http://your-laravel-app/api/crawl
- 請求參數:
url
: 要抓取的網址
5. 進階應用:
- 驗證請求參數:
- 使用 Laravel 的驗證功能來驗證請求參數,確保資料的正確性。
- 設定 API 權限:
- 使用 Laravel Sanctum 或 JWT 等套件來設定 API 權限,保護 API 的安全性。
- 處理錯誤:
- 加入錯誤處理機制,例如處理網路錯誤、網站結構變更等,並返回適當的錯誤訊息。
- 設定 API 速率限制:
- 使用 Laravel 的速率限制功能來限制 API 的請求頻率,避免過度使用。
- 快取資料:
- 將抓取到的資料快取起來,減少對目標網站的請求次數。
透過以上步驟,您就可以將 Laravel 爬蟲功能轉換為 API,讓其他應用程式或服務可以使用您的爬蟲功能。
沒有留言:
張貼留言