Download the PHP package changhorizon/crawler without Composer
On this page you can find all versions of the php package changhorizon/crawler. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download changhorizon/crawler
More information about changhorizon/crawler
Files in changhorizon/crawler
Package crawler
Short Description Effortless recursive web crawling for comprehensive site indexing
License MIT
Informations about the package crawler
Crawler
Effortless recursive web crawling for comprehensive site indexing
A high-performance PHP web crawler library designed to recursively scrape all pages of a single website.This library is ideal for building site search, content analysis, SEO data collection, and similar projects. It is designed to be simple, extensible, and serve as a solid foundation for web crawling and data extraction.
✨ 特性
- 递归爬取:自动提取页面内链接,广度优先遍历站点所有可访问页面
- 内容解析:提取页面标题、关键词、描述以及正文文本,过滤无用标签和脚本
- 增量更新:通过内容哈希值检测页面变更,避免重复写入数据库,提高效率
- 日志记录:内置 PSR-3 日志接口,方便集成多种日志方案,实时跟踪爬取过程
- 多语言支持:自动识别页面语言(lang属性),便于多语站点索引和处理
- 持久化存储:采用 PDO 操作 MySQL,支持页面数据入库,方便后续搜索索引或分析
- 可配置延时:支持爬取间隔时间设置,降低对目标站点的压力
📦 安装
📂 目录结构
🚀 用法示例
示例 1:基础爬取示例
示例 2:自定义爬取间隔
📐 接口说明
Crawler::__construct
构造函数,创建一个爬虫实例。
- $startUrl:起始 URL。
- $httpClient:实现
HttpClientInterface的 HTTP 客户端 - $storage:实现
DocumentStorageInterface的文档存储实现 - $logger:实现
LoggerInterface(PSR-3 标准)的日志处理器 - $crawlDelay:每次抓取之间的间隔,单位为秒
Crawler::run
启动爬虫任务。
Crawler::fetchUrlContent
获取 HTML 页面正文内容,过滤掉响应头,若请求无效返回 null。
Crawler::scrapePage
抓取并解析页面,存储内容,并递归抓取内部链接。
Crawler::extractLinks
从 HTML 中提取合法的绝对链接。
Crawler::resolveUrl
将相对链接解析为绝对 URL,仅保留与起始 URL 同源的链接。
ChangHorizon\Crawler\Storage\DocumentStorageInterface
定义用于文档存储的接口,包含存储与状态检测方法。
ChangHorizon\Crawler\Http\HttpClientInterface
定义用于发起 HTTP 请求的接口。
备注: 所有接口实现均来自包内组件,适合用于扩展和替换。爬虫本体逻辑通过 DocumentStorageInterface 和 HttpClientInterface 解耦,便于测试与复用。
🔍 静态分析
使用 PHPStan 工具进行静态分析,确保代码的质量和一致性:
🎯 代码风格
使用 PHP-CS-Fixer 工具检查代码风格:
使用 PHP-CS-Fixer 工具自动修复代码风格问题:
✅ 单元测试
执行 PHPUnit 单元测试:
执行 PHPUnit 单元测试并生成代码覆盖率报告:
🤝 贡献指南
欢迎 Issue 与 PR,建议遵循以下流程:
- Fork 仓库
- 创建新分支进行开发
- 提交 PR 前请确保测试通过、风格一致
- 提交详细描述
📜 License
MIT License. See the LICENSE file for details.