nws / ultra-parser
Laravel 扩展包,用于轻松抓取网页
2.2.3
2019-05-06 10:06 UTC
Requires
- php: >=7.1
- electrolinux/phpquery: 0.9.*
- guzzlehttp/guzzle: 6.*
- laravel/framework: ~5.2|~5.2.32|~5.3.0|~5.4.0|~5.5.0|~5.6.0|~5.7.0
This package is auto-updated.
Last update: 2020-06-10 17:41:51 UTC
README
此版本的 README 已过时... 新版本不可公开
这是一个 laravel 扩展包,它使网站解析变得非常简单。从任何地方解析一切,只需创建一个配置文件。
Composer
composer require nws/ultra-parser
使用方法
1. 发布配置
php artisan vendor:publish --tag="ultra-parser"
执行此命令后,ultra-parser 的配置将发布到您的 /config 文件夹。
检查它并运行安装前进行更改。
2. 安装
php artisan ultra-parser:install
此命令将根据您的 config 文件创建 models 并运行迁移。
3. 运行链接解析器
可用参数。
| 参数 | 类型 | 必需 | 描述 |
|---|---|---|---|
| --key | 字符串 | + | 来自配置文件的网站密钥 |
| --from | 整数 | + | 从页面编号开始解析 |
| --to | 整数 | + | 解析到页面编号 |
| --config | 字符串 | - | 配置文件名 |
| --force | 空 | - | 用于解析已解析的页面 |
| --threads | 整数 | - | 线程数 |
| --timeout | 整数 | - | 页面解析之间的超时时间 |
| --debug | 空 | - | 打印第一个解析结果 |
示例
php artisan ultra-parser:links --key=ivi --from=0 --to=10 --threads=5 --force
4. 运行数据解析器
可用参数。
| 参数 | 类型 | 必需 | 描述 |
|---|---|---|---|
| --key | 字符串 | + | 来自配置文件的网站密钥 |
| --count | 整数 | - | 每个线程解析数据的链接数量(1000) |
| --config | 字符串 | - | 配置文件名 |
| --force | 空 | - | 用于解析已解析的页面 |
| --threads | 整数 | - | 线程数 |
| --timeout | 整数 | - | 页面解析之间的超时时间 |
| --debug | 空 | - | 打印第一个解析结果 |
示例
php artisan ultra-parser:data --key=ivi --threads=5 --count=100 --force
说明
>运行具有密钥 ivi 的站点配置的数据解析器。
>使用 5 个线程,选择 status 在 (0, 2) 之间的链接,每个线程限制 100,最终将解析 500 个链接。
配置
配置文件示例
<?php
return [
'tables' => [
'links' => 'parsed_links',
'data' => 'parsed_data',
],
'models' => [
'links' => 'App\Models\ParsedLink',
'data' => 'App\Models\ParsedData'
],
'sites' => [
'ivi' => [ // Key
'main_url' => 'https://www.ivi.ru/', // Site url
'links' => [ // Links parsing rules
'type' => 'page', // Type (can be 'page' or 'from_to'
'rules' => [ // Rules
'url' => 'https://www.ivi.ru/movies/', // main link
'page_param' => 'page{$page}', // page param pattern
'parse_rules' => [ // parsing rules
'type' => 'html',
'selector' => '.poster-badge > a', // Selector of element
'content' => 'href' // Concrete attribute from where to get link
]
]
],
'data' => [ // Data parsing rules
'image' => [ // Key
'type' => 'html',
'selector' => 'video-info', // Selector of element
'content' => 'data-poster' // Concrete attribute from where to get data
],
'title' => [
'type' => 'html',
'selector' => 'video-info',
'content' => 'data-title'
],
'description' => [ // Key
'type' => 'html',
'selector' => '.description[itemprop="description"] p', // Selector of element
'content' => '#text',
'extra' => 'implode' //
]
]
]
],
'timeout' => 5, // Sleep after parsing 1 page
'proxy_servers' => [] // Proxy ips
];
有三种全局规则类型,html、regexp、json 和 collector。
对于类型 html 使用 selector。
对于类型 regexp 使用 pattern。
对于类型 json 使用 key_chain 和 main_key。
content 规则可以是简单的属性名称或文本节点,也可以是更高级的配置。
例如,您想要解析并获取 <p> 和 <h3> 的文本节点作为数据。
<div class="main_container">
<span>I don't need this</span>
<span>I don't need this</span>
<span>I don't need this</span>
<p>I NEED THIS</p>
<span>I don't need this</span>
<h3>I NEED THIS TOO</h3>
</div>
您的配置必须如下所示
'type' => 'collector',
'from' => [
'type' => 'html',
'selector' => '.main_container'
],
'collect' => [
[
'type' => 'html',
'selector' => 'p',
'content' => '#text'
],
[
'type' => 'html',
'selector' => 'h3',
'content' => '#text'
]
],
'extra' => 'implode'
或者如果您的 HTML 看起来像这样
<div class="main_container">
<span>I don't need this</span>
<span>I don't need this</span>
<span>I don't need this</span>
<p>I NEED THIS</p>
<span>I don't need this</span>
<h3 data-content="I NEED THIS TOO"></h3>
</div>
您的配置必须如下所示
'type' => 'collector'
'from' => [
'type' => 'html',
'selector' => '.main_container'
],
'collect' => [
[
'type' => 'html',
'selector' => 'p',
'content' => '#text' // textContent
],
[
'type' => 'html'
'selector' => 'h3',
'content' => 'data-content' //getAttribute('data-content')
]
]
使用正则表达式解析的配置示例
配置
'data' => [
'image' => [
'type' => 'regexp',
'pattern' => '/<img\s+class=\"image\"\s+src=\"(.*)?\"/',
'content' => 1 //Key from preg_match_all results
]
]
从 JSON 解析链接的配置示例
配置
'links' => [ // Links parsing rules
'type' => 'from_to',
'rules' => [
'url' => 'https://api.ivi.ru/mobileapi/catalogue/v5/?sort=pop&genre_operator=and&category=14&fields=id&app_version=2268&session=cc59e9105793659549597370_1567009192-0Y-qrstvPnybiFr6esx6glw',
'from' => 'from={$from}',
'to' => 'to={$to}',
'parse_rules' => [
'type' => 'json',
'key_chain' => 'result',
'main_key' => 'id',
'value_prefix' => '/watch/'
]
]
]
如何在不更改配置文件的情况下进行测试?
您可以将测试配置文件名作为 --config 参数传递给解析器命令。
创建文件 app/parser_configs/ivi_parser.php,内容如下
<?php
return [
'ivi' => [
'main_url' => 'https://www.ivi.ru/', // Site url
'links' => [ // Links parsing rules
'type' => 'page', // Type (can be 'page' or 'from_to'
'rules' => [ // Rules
'url' => 'https://www.ivi.ru/movies/', // main link
'page_param' => 'page{$page}', // page param pattern
'parse_rules' => [ // parsing rules
'type' => 'html',
'selector' => '.poster-badge > a', // Selector of element
'content' => 'href' // Concrete attribute from where to get link
]
]
],
'data' => [ // Data parsing rules
'image' => [ // Key
'type' => 'html',
'selector' => 'video-info', // Selector of element
'content' => 'data-poster' // Concrete attribute from where to get data
],
'title' => [ // Key
'type' => 'html',
'selector' => 'video-info', // Selector of element
'content' => 'data-title' // Concrete attribute from where to get data
],
'description' => [ // Key
'type' => 'html',
'selector' => '.description[itemprop="description"] p', // Selector of element
'content' => '#text',
'extra' => 'implode'
]
]
]
];
然后运行类似以下命令
php artisan ultra-parser:data --config=app/parser_configs/ivi_parser.php --key=ivi
额外字段的合法值
- trim:清除空白字符
- strip_tags:移除 HTML 标签
- implode:将结果连接成一个字符串
重要!!!
更改主要配置文件后,不要忘记运行 >>
php artisan config:clear