Laravel Scraper is a flexible and powerful web scraping package for Laravel applications. It provides a unified interface to interact with multiple scraping services, allowing you to easily switch between or combine different scraping APIs based on your needs.
You can install the package via composer:
composer require gregpriday/laravel-scraperAfter installation, publish the configuration file:
php artisan vendor:publish --provider="GregPriday\Scraper\ScraperServiceProvider"This will create a config/scraper.php file where you can configure your scraping services.
Laravel Scraper supports multiple scraping services. Here's how to configure some popular ones:
- Sign up for an account at ScrapingBee
- Get your API key from the dashboard
- Add the following to your
.envfile:
SCRAPINGBEE_API_KEY=your_api_key_here
- Create an account at Zyte
- Obtain your API key
- Add to your
.envfile:
ZYTE_API_KEY=your_api_key_here
Here's how to make a basic scraping request:
use GregPriday\Scraper\Facades\Scraper;
$response = Scraper::get('https://example.com');
$content = $response->getBody();
$statusCode = $response->getStatusCode();
$headers = $response->getHeaders();Laravel Scraper can be easily integrated with Spatie's Crawler. Here's a quick example:
use Spatie\Crawler\Crawler;
use GregPriday\Scraper\Facades\Scraper;
Crawler::create()
->setCrawlObserver(YourCrawlObserver::class)
->setClient(Scraper::getClient())
->startCrawling('https://example.com');This sets up the crawler to use Laravel Scraper for all requests, benefiting from its multi-service capabilities and automatic retries.
You can add or modify scraping services in the config/scraper.php file. Each service can have its own configuration and priority:
return [
'scrapers' => [
'scrapingbee' => [
'driver' => 'scrapingbee',
'api_key' => env('SCRAPINGBEE_API_KEY'),
'priority' => 10,
],
'zyte' => [
'driver' => 'zyte',
'api_key' => env('ZYTE_API_KEY'),
'priority' => 20,
],
// Add more scrapers here
],
];The priority determines the order in which scrapers are attempted, with lower numbers being tried first.
You can create custom scrapers by implementing the ScraperInterface or extending the AbstractScraper class.
Laravel Scraper will automatically try the next scraper in the stack if one fails. You can catch exceptions at the application level:
use GregPriday\Scraper\Exceptions\ScraperException;
try {
$response = Scraper::get('https://example.com');
} catch (ScraperException $e) {
// Handle the exception
}The Laravel Scraper package is open-sourced software licensed under the MIT license.