lobotomised / laravel-autocrawler
一个用于爬取您的laravel安装并检查HTTP状态码的工具
Requires
- php: ^8.1
- illuminate/console: ^10.0|^11.0
- illuminate/filesystem: ^10.0|^11.0
- spatie/crawler: ^8.2
Requires (Dev)
- nunomaduro/collision: ^6.2|^8.0
- nunomaduro/larastan: ^2.1|^2.4
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^1.21|^2.34
- pestphp/pest-plugin-laravel: ^1.2|^2.3
- phpunit/phpunit: ^9.5|^10.5
This package is auto-updated.
Last update: 2024-09-17 07:51:59 UTC
README
使用此包,您可以检查您的应用程序是否有断链。
php artisan crawl 200 OK - http://myapp.test/ 200 OK - http://myapp.test/login found on http://myapp.test/ 200 OK - http://myapp.test/register found on http://myapp.test/ 301 301 Moved Permanently - http://myapp.test/homepage found on http://myapp.test/register 404 Not Found - http://myapp.test/brokenlink found on http://myapp.test/register 200 OK - http://myapp.test/features found on http://myapp.test/ Crawl finished Results: Status 200: 4 founds Status 301: 1 found Status 404: 1 found
安装
此包可以通过Composer安装
composer require --dev lobotomised/laravel-autocrawler
当爬取您的网站时,它将自动检测您的应用程序使用的URL。如果它扫描 https://,请检查您的.env文件,确保已正确配置APP_URL变量
APP_URL="http://myapp.test"
使用
爬取特定URL
默认情况下,爬虫将从您的当前laravel安装中爬取URL。您可以使用--url
选项强制使用URL
php artisan crawl --url=http://myapp.test/my-page
并发连接
爬虫以10个并发连接运行以加快爬取过程。您可以通过传递--concurrency
选项来更改此值
php artisan crawl --concurrency=5
超时
请求超时默认为30秒。使用--timeout
更改此值
php artisan crawl --timeout=10
忽略robots.txt
默认情况下,爬虫会尊重robots.txt。您可以使用--ignore-robots
选项忽略这些规则
php artisan crawl --ignore-robots
外部链接
当爬虫发现外部链接时,它将检查此链接。您可以使用--ignore-external-links
选项将其禁用
php artisan crawl --ignore-external-links
记录非2xx或非3xx状态码
默认情况下,爬虫将在控制台中进行。您可以使用--output
选项将所有非2xx或非3xx状态码记录到文件中。结果将存储在storage/autocrawler/output.txt
php artisan crawl --output
output.txt将如下所示
403 Forbidden - http://myapp.test/dashboard found on http://myapp.test/home
404 Not Found - http://myapp.test/brokenlink found on http://myapp.test/register
当发现非2xx或非3xx时失败
默认情况下,命令退出码为0。您可以使用--fail-on-error
将其更改为1以指示命令已失败
php artisan crawl --fail-on-error
以交互方式启动机器人
最终,您可以通过使用--interactive
选项以交互方式配置爬虫
php artisan crawl --interactive
与GitHub Actions一起使用
要执行爬虫,您首先需要启动一个web服务器。您可以选择安装apache或nginx。以下是一个使用内置php web服务器的示例
如果爬取发现一些非2xx或非3xx响应,则操作将失败,并将结果存储为操作的工作成果
steps:
- uses: actions/checkout@v3
- name: Prepare The Environment
run: cp .env.example .env
- name: Install Composer Dependencies
run: composer install
- name: Generate Application Key
run: php artisan key:generate
- name: Install npm Dependencies
run: npm ci
- name: Compile assets
run: npm run build
- name: Start php build-in webserver
run: (php artisan serve &) || /bin/true
- name: Crawl website
run: php artisan crawl --url=https://:8000/ --fail-on-error --output
- name: Upload artifacts
if: failure()
uses: actions/upload-artifact@master
with:
name: Autocrawler
path: ./storage/autocrawler
文档
所有命令和信息都可以使用命令获得
php artisan crawl --help
替代方案
此包受到spatie/http-status-check的很大启发,但与作为项目依赖项不同,它是一个全局安装
测试
首先,我们需要在一个单独的终端中启动包含的node http服务器。
make start
然后运行测试
make test
变更日志
请参阅CHANGELOG以获取有关最近更改的更多信息。
许可
MIT许可证(MIT)。有关更多信息,请参阅许可文件。