vipnytt / robotstxtparser
Robots.txt 解析库,支持每个指令和规范。
v2.1.0
2021-04-10 16:59 UTC
Requires
- php: ^7.3 || ^8.0
- ext-curl: *
- ext-mbstring: *
- composer/ca-bundle: ^1.0
- vipnytt/useragentparser: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-11 00:35:08 UTC
README
Robots.txt 解析器
一个易于使用、可扩展的 robots.txt
解析库,完全支持互联网上每一个 指令 和 规范。
用例
- 权限检查
- 获取爬虫规则
- 发现 Sitemap
- 主机偏好
- 动态 URL 参数发现
- 渲染
robots.txt
优点
(与其他大多数 robots.txt 库相比)
- 自动下载
robots.txt
。 (可选) - 集成 缓存系统。 (可选)
- 爬取 延迟处理。
- 文档 可用。
- 支持每一个 指令,来自 每一个规范。
- HTTP 状态码处理器,根据 Google 的规范。
- 专门的
User-Agent
解析器和组确定库,以实现最大精确度。 - 提供额外数据,如 首选主机、动态 URL 参数、Sitemap 位置等。
- 支持的协议:
HTTP
、HTTPS
、FTP
、SFTP
和FTP/S
。
要求
安装
安装 robots.txt 解析器推荐的方法是通过 Composer。将其添加到您的 composer.json
文件中
{ "require": { "vipnytt/robotstxtparser": "^2.1" } }
然后运行: php composer update
入门
基本用法示例
<?php $client = new vipnytt\RobotsTxtParser\UriClient('http://example.com'); if ($client->userAgent('MyBot')->isAllowed('http://example.com/somepage.html')) { // Access is granted } if ($client->userAgent('MyBot')->isDisallowed('http://example.com/admin')) { // Access is denied }
一些基本方法的摘录
<?php // Syntax: $baseUri, [$statusCode:int|null], [$robotsTxtContent:string], [$encoding:string], [$byteLimit:int|null] $client = new vipnytt\RobotsTxtParser\TxtClient('http://example.com', 200, $robotsTxtContent); // Permission checks $allowed = $client->userAgent('MyBot')->isAllowed('http://example.com/somepage.html'); // bool $denied = $client->userAgent('MyBot')->isDisallowed('http://example.com/admin'); // bool // Crawl delay rules $crawlDelay = $client->userAgent('MyBot')->crawlDelay()->getValue(); // float | int // Dynamic URL parameters $cleanParam = $client->cleanParam()->export(); // array // Preferred host $host = $client->host()->export(); // string | null $host = $client->host()->getWithUriFallback(); // string $host = $client->host()->isPreferred(); // bool // XML Sitemap locations $host = $client->sitemap()->export(); // array
以上只是基本的味道,还有许多更高级和/或专业的方 法可用于几乎任何目的。访问 速查表 获取技术细节。
访问 文档 获取更多信息。
指令
规范
- Google robots.txt 规范
- Yandex robots.txt 规范
- W3C 建议HTML 4.01 规范
- Sitemaps.org 协议
- Sean Conner: "An Extended Standard for Robot Exclusion"
- Martijn Koster: "A Method for Web Robots Control"
- Martijn Koster: "A Standard for Robot Exclusion"
- RFC 7231,
2616 - RFC 7230,
2616 - RFC 5322,
2822,822 - RFC 3986,
1808 - RFC 1945
- RFC 1738
- RFC 952