baqend/spider
Composer 安装命令:
composer require baqend/spider
包简介
URL spider which crawls a page and all its subpages
README 文档
README
URL spider which crawls a page and all its subpages
Installation
Make sure you have Composer installed. Then execute:
composer require baqend/spider
This package requires at least PHP 5.5.9 and has no package dependencies!
Usage
The entry point is the Spider class. For it to work, it requires the following services:
- Queue: Collects URLs to be processed. This package comes with a breadth-first and a depth-first implementation.
- URL Handler: Checks if a URL should be processed. If no URL handler is provided, every URL is processed. More about URL handlers
- Downloader: Takes URLs and downloads them. To have no dependency on a HTTP client library like Guzzle, you have to implement this class by yourself.
- Processor: Retrieves downloaded assets and performs operations on it. More about Processors
You initialize the spider in the following way:
<?php use Baqend\Component\Spider\Processor; use Baqend\Component\Spider\Queue\BreadthQueue; use Baqend\Component\Spider\Spider; use Baqend\Component\Spider\UrlHandler\BlacklistUrlHandler; // Use the breadth-first queue $queue = new BreadthQueue(); // Implement the DownloaderInterface $downloader /* your downloader implementation */; // Create a URL handler, e.g. the provided blacklist URL handler $urlHandler = new BlacklistUrlHandler(['**.php']); // Create some processors which will be executed after another // More details on the processors below! $processor = new Processor\Processor(); $processor->addProcessor(new Processor\UrlRewriteProcessor('https://example.org', 'https://example.com/archive')); $processor->addProcessor($cssProcessor = new Processor\CssProcessor()); $processor->addProcessor(new Processor\HtmlProcessor($cssProcessor)); $processor->addProcessor(new Processor\ReplaceProcessor('https://example.org', 'https://example.com/archive')); $processor->addProcessor(new Processor\StoreProcessor('https://example.com/archive', '/tmp/output')); // Create the spider instance $spider = new Spider($queue, $downloader, $urlHandler, $processor); // Enqueue some URLs $spider->queue('https://example.org/index.html'); $spider->queue('https://example.org/news/other-landingpage.html'); // Execute the crawling $spider->crawl();
Processors
This package comes with the following built-in processors.
Processor
This is an aggregate processor which allows adding and removing other processors which it will execute one after the other.
<?php use Baqend\Component\Spider\Processor\Processor; $processor = new Processor(); $processor->addProcessor($firstProcessor); $processor->addProcessor($secondProcessor); $processor->addProcessor($thirdProcessor); // This will call `process` on $firstProcessor, $secondProcessor, and finally on $thirdProcessor: $processor->process($asset, $queue);
HtmlProcessor
This processor can process HTML assets and enqueue its containing URLs.
It will also modify all relative URLs and make them absolute.
Also, if you provide a CssProcessor, style attributes are found and URLs within CSS will be resolved.
CssProcessor
This processor can process CSS assets and enqueue its containing URLs from @imports and url(...) statements.
ReplaceProcessor
Performs simple str_replace operations on asset contents:
<?php use Baqend\Component\Spider\Processor\ReplaceProcessor; $processor = new ReplaceProcessor('Hello World', 'Hallo Welt'); // This will replace all occurrences of // "Hello World" in the asset with "Hallo Welt": $processor->process($asset, $queue);
The ReplaceProcessor does not enqueue other URLs.
StoreProcessor
Takes a URL prefix and a directory and will store all assets relative to the prefix in the according file structure in directory.
The StoreProcessor does not enqueue other URLs.
UrlRewriteProcessor
Changes the URL of an asset to another prefix. Use this to let HtmlProcessor and CssProcessor resolve relative URLs from a different origin.
The UrlRewriteProcessor does not enqueue other URLs.
Also, it does not modify the asset's content – only its URL.
URL Handlers
URL handlers tell the spider whether to download and process a URL. There are the following built-in URL handlers:
OriginUrlHandler
Handles only URLs coming from some given origin, i.e. "https://example.org".
BlacklistUrlHandler
Does not handle URLs being part of some blacklist. You can use glob patterns to provide a blacklist:
<?php use Baqend\Component\Spider\UrlHandler\BlacklistUrlHandler; $blacklist = [ 'https://other.org/**', // Don't handle anything from other.org over HTTPS 'http{,s}://other.org/**', // Don't handle anything from other.org over HTTP or HTTPS '**.{png,gif,jpg,jpeg}', // Don't handle any image files ]; $urlHandler = new BlacklistUrlHandler($blacklist);
Alternatives
If this project does not match your needs, check the following other projects:
- spatie/crawler (Requires PHP 7)
- vdb/php-spider
baqend/spider 适用场景与选型建议
baqend/spider 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 625 次下载、GitHub Stars 达 6, 最近一次更新时间为 2018 年 03 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 baqend/spider 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 baqend/spider 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 625
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-03-16