octopoda / 章鱼
PHP 网站地图爬虫
0.11.0
2024-01-22 09:31 UTC
Requires
- php: ~8.3
- ext-simplexml: *
- clue/reactphp-flux: ^1.4
- psr/log: ^2.0 || ^3.0
- react/filesystem: ^0.1
- react/http: ^1.9
- symfony/console: ^6.0 || ^7.0
- teapot/status-code: ^2.2
Requires (Dev)
- friendsofphp/php-cs-fixer: @stable
- phpstan/phpstan: @stable
- phpunit/phpunit: @stable
- rector/rector: @stable
- roave/security-advisories: dev-master
README
Small PHP tool to crawl collections of URLs in a Sitemap using the PHPReact library for asynchronous loading of the URLs. Both plain text files and XML Sitemaps are supported.
通过命令行界面(CLI)的使用
使用详细日志记录(-vvv)爬取网站地图中的 URL。
php application.php http://www.domain.ext/sitemap.xml -vvv
使用 15 个并发连接而不是默认的 5 个并发连接
php application.php http://www.domain.ext/sitemap.xml --concurrency 15 -vvv
使用 HTTP GET
请求而不是默认的 HTTP HEAD
。注意,HTTP HEAD
请求涉及更少的数据传输,因为不涉及主体
php application.php http://www.domain.ext/sitemap.xml --requestType GET -vvv
使用 3 秒的超时时间而不是默认的 10 秒
php application.php http://www.domain.ext/sitemap.xml --timeout 3 -vvv
使用特定的 UserAgent 而不是默认的 Octopus/1.0
,例如,模拟搜索引擎爬取网站地图
php application.php http://www.domain.ext/sitemap.xml --userAgent 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' -vvv
使用 TablePresenter
来显示中间结果而不是默认的 EchoPresenter
php application.php http://www.domain.ext/sitemap.xml --presenter Octopus\\Presenter\\TablePresenter -vvv
在你的应用程序中使用
你可以轻松地将网站地图爬取集成到自己的应用程序中,请查看 Config
类以了解所有可能的配置选项。如有必要,你可以使用 PSR3-Logger 进行日志记录。
use Octopus\Config; use Octopus\Processor; $config = new Config(); $config->concurrency = 2; $config->targetFile = 'https://www.domain.ext/sitemap.xml'; $config->additionalResponseHeadersToCount = array( 'CF-Cache-Status', //Useful to check CloudFlare edge server cache status ); $config->requestHeaders = array( 'User-Agent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', //Simulate Google's webcrawler ); $processor = new Processor($config, $this->logger); //A PSR3 Logger can be injected if required $processor->run(); $this->logger->info('Statistics: ' . print_r($processor->result->getStatusCodes(), true)); $this->logger->info('Applied concurrency: ' . $config->concurrency); $this->logger->info('Total amount of processed data: ' . $processor->result->getTotalData()); $this->logger->info('Failed to load #URLs: ' . count($processor->result->getBrokenUrls()));
限制
目前,Octopus 主要是一个实验性/教育性工具。在 HTTP 响应处理中的高级用例可能不受支持。
测试
要运行测试套件,您首先需要克隆此存储库,然后使用 Composer 安装所有依赖项 使用 Composer
$ composer install
要运行测试套件,请转到项目根目录并运行
$ make test