gyaaniguy/pcrawl
Composer 安装命令:
composer require gyaaniguy/pcrawl
包简介
PHP web scraping and crawling library. With support for multiple clients, fast parsing, debugging and on the fly changes to various options
README 文档
README
PCrawl
PCrawl is a PHP library for crawling and scraping web pages.
It supports multiple clients: curl, guzzle. Options to debug, modify and parse responses.
Features
- Rapidly create custom clients. Fluently change clients and client options like user-agent, with method chaining.
- Responses can be modified using reusable callback functions.
- Debug Responses using different criterias - httpcode, regex etc.
- Parse responses using querypath library. Several convenience functions are provided.
- Fluent API. Different debuggers, clients and response mod objects can be be changed on the fly !
Full Example
We'll try to fetch a bad page, then detect using a debugger and finally change client options to fetch the page correctly.
- Setup up some clients
// simple clients. $gu = new GuzzleClient(); // Custom Client, that does not allow redirects. $uptightNoRedirectClient = new CurlClient(); $uptightNoRedirectClient->setRedirects(0); // disable redirects // Custom client - thin wrapper around curl class ConvertToHttpsClient extends CurlClient { public function get(string $url, array $options = []): PResponse { $url = str_replace('http://', 'https://', $url); return parent::get($url, $options); } }
- Lets make some debugger objects
$redirectDetector = new ResponseDebug(); $redirectDetector->setMustNotExistHttpCodes([301, 302, 303, 307, 308]); $fullPageDetector = new ResponseDebug(); $fullPageDetector->setMustExistRegex(['#</html>#']);
Start fetching!
For testing, we will fetch page with a client that does not support redirects, then use the redirectDetector to detect 301. If so we change client option to support redirects and fetch again.
$req = new Request(); $url = "http://www.whatsmyua.info"; $req->setClient($uptightNoRedirectClient); $count = 0; do { $res = $req->get($url); $redirectDetector->setResponse($res); if ($redirectDetector->isFail()) { var_dump($redirectDetector->getFailDetail()); $uptightNoRedirectClient->setRedirects(1); $res = $req->get($url); } } while ($redirectDetector->isFail() && $count++ < 1);
Use the fullPageDetector to detect if the page is proper.
Then parse the response body using Parser
if ($fullPageDetector->setResponse($res)->isFail()) { var_dump($redirectDetector->getFailDetail()); } else { $parser = new ParserCommon($res->getBody()); $h1 = $parser->find('h1')->text(); $htmlClass = $parser->find('html')->attr('class'); }
Note: the debuggers, clients, parsers can be reused.
Detailed Usage
Usage of functions can be divided into parts:
Installation
- Composer:
composer init # for new projects. composer config minimum-stability dev # Will be removed once stable. composer require gyaaniguy/pcrawl composer update include __DIR__ . '/vendor/autoload.php'; #in PHP
- github:
git clone git@github.com:gyaaniguy/PCrawl.git # clone repo cd PCrawl composer update # update composer mv ../PCrawl /desired/location # Move dir to desired location. require __DIR__ . '../PCrawl/vendor/autoload.php'; #in PHP
TODO list
- Leverage guzzlehttp asynchronous support
Standards
PSR-12
PHPUnit tests
gyaaniguy/pcrawl 适用场景与选型建议
gyaaniguy/pcrawl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 03 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「crawler」 「scraping」 「web crawler」 「web scraping」 「php web scraping」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gyaaniguy/pcrawl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gyaaniguy/pcrawl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gyaaniguy/pcrawl 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A lightweight, dependency free PHP class that acts as wrapper for Crawlbase API
Sitemap crawler/generator. For the given URL it will return sitemap XML file with URLs and images.
Bing Image Extractor
An SDK designed for easy usage of the allrival.com website's API
Alfabank REST API integration
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-4-Clause
- 更新时间: 2023-03-05