shamanhead/phpporser
Composer 安装命令:
composer require shamanhead/phpporser
包简介
Advanced parser for advanced tasks
README 文档
README
-
Getting Started
- Requirements
- Installing via composer
- Installing via archive
- Installing chromium executable
- Parsing your first page
- Search methods
- Working with text
- Contribute
- License
Getting Started
Requirements
Requires PHP 7.1+.Also needs Headless Chromium PHP and chromium executable, if you want to use this library with headless browsing support(includes by standart in packagist version).
Installing via composer
$ composer require shamanhead/phpporser
Installing via archive
You can install this library also from archive, by downloading it from github. There is no dependendies needed, besides Headless Chromium PHP, if you want to use this library with headless browsing feature. Installing chromium executable If you want to use this library with headless browser, first you need to download executable of this browser.This might works on Windows, MacOs and Linux.
Choose browser that you want to use
Headless chromium supports all chomium-based browsers, like Chrome, Opera, Chromium etc.Installing chromium executable
I can recommend to use chromium instead of chrome, because of my observation he works better than chrome.So, go on the official chromium browser downloading page and download it.
After doing this step, unpack archive and move to necessary place.
Then, specify path in your script:
require_once "vendor/autoload.php"; use HeadlessChromium\Page; use ShamanHead\PhpPorser\App\Dom as Dom; $dom = new Dom(); $dom->setHref('file:///home/shamanhead/dev/porser/phpporser-master/test.html'); $dom->setBrowserPath('PATH_TO_CHROME');
If you done all right, parser would work. If you have any errors occuring during this step, you can go see here, is there solution to solve your problem. In other case, please, open new issue here or on Headless Chromium PHP page.
Parsing your first page
Huh, half of work done. So now, let's try to parse simple page, like Computer sciense on wikipedia. With the help of it, I will show all the capabilities of the parser.First of all, let's try to get 'Computer sciense' string on top of the page:
<?php require_once "vendor/autoload.php"; use ShamanHead\PhpPorser\App\Dom as Dom; $dom = new Dom(); $dom->setHref('https://en.wikipedia.org/wiki/Computer_science'); print_r($dom->tag('h1')->class('firstHeading')->text()->merge()); ?>
It's works! But how? Let's me explain:
- Parser get's all tags with name 'h1'
- Then parser get's all tags with class 'firstHeading' in h1 tags range(and it's dependencies)
- Get's text from it
- Converts result array to string format
Search methods
To find elements in html dom, there is 4 functions in this library:<?php require_once "vendor/autoload.php"; use ShamanHead\PhpPorser\App\Dom as Dom; $dom = new Dom(); $dom->setHref('href to file'); print_r($dom->tag('h1')->array()); //finds by tag name 'h1' print_r($dom->id('firstHeading')->array()); //finds by id name 'firstHeading' print_r($dom->class('wrapper__main')->array()); //finds by class name 'wrapper_main' print_r($dom->custom(['name', 'button'])->array()); //finds by 'name' attribute value 'button' ?>
You can combine search methods with each other, to find elements in special way:
<?php require_once "vendor/autoload.php"; use ShamanHead\PhpPorser\App\Dom as Dom; $dom = new Dom(); $dom->setHref('href to file'); print_r($dom->class('main')->id('firstHeading')->tag('h1')->array()); ?>
Working with text
<?php require_once "vendor/autoload.php"; use ShamanHead\PhpPorser\App\Dom as Dom; $dom = new Dom(); $dom->setHref('href to file'); $divText = $dom->tag('div')->id('someDiv')->text(); $divText->contents(); //Returns all text in array form. $divText->merge('symbol'); //Returns all text in string form with 'symbol' separator //'\n' by default. $divText->first(); //Returns first founded text. $divText->last(); //Returns last founded text. ?>
Contribute
Hey, want to contribute? Just notice me on my email ( arsenii.romanovskii85@gmail.com ), where will you indicate what you want to help.License
See the LICENSE file.shamanhead/phpporser 适用场景与选型建议
shamanhead/phpporser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 256 次下载、GitHub Stars 达 7, 最近一次更新时间为 2021 年 04 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「php」 「library」 「parsing」 「pars」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 shamanhead/phpporser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 shamanhead/phpporser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 shamanhead/phpporser 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Inbox pattern process implementation for your Laravel Applications
Simple ASCII output of array data
Core library that defines common interfaces used by the rest of the intahwebz..
Amqp classes
Provides command to manage a web library directory
统计信息
- 总下载量: 256
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: AGPL-3.0-or-later
- 更新时间: 2021-04-22