userforce / scraper
抓取网页并结构化结果。
v1.1.2
2019-07-09 22:45 UTC
Requires
- php: >=7.1
- guzzlehttp/guzzle: >=5.0
- illuminate/support: >=5.5
This package is not auto-updated.
Last update: 2024-09-26 00:21:37 UTC
README
使用正则表达式抓取网页并结构化结果。
安装
使用Composer要求此包
composer require userforce/scraper
在Laravel中注册Scraper。打开config/app.php,在提供者列表末尾添加UserForce\ScraperServiceProvider
'providers' => [ ... UserForce\Scraper\ScraperServiceProvider::class, ],
然后在同一config/app.php中别名列表末尾添加UserForce\Facade\Scraper
'aliases' => [ ... 'Scraper' => UserForce\Scraper\Facade\Scraper::class, ],
现在您可以开始使用Scraper了
用法
use Scraper;
Scraper有一个名为find
的方法,接受一个参数
$result = Scraper::find($config);
示例
每个配置选项必须有两个键url
和regex
。
您可以在树中定义多个配置选项(结构将保持不变)。
此外,regex
可以是字符串或关联数组,但不能为空。每个字符串都将被解释为正则表达式。
$config = [ 'ibmachine' => [ 'url' => 'https://ibmachine.com/machine', 'regex' => [ 'name' => 'machine\/view\/[0-9]{1,7}" itemprop="name">\s*(<span.*\/span>)?\s*(.*)\s*<\/a>', 'links' => [ 'url' => 'href=\"(http.*machine\/view\/[\d]{1,7})\"\sitemprop' ] ] ] ]; $result = Scraper::find($config); $result->get();
结果
array:1 [▼ "ibmachine" => array:2 [▼ "name" => array:3 [▼ 0 => array:20 [▶] 1 => array:20 [▶] 2 => array:20 [▼ 0 => "Alesatrice TOS Whn q 13 anno 2001" 1 => "CURVATRICE TAURING mod. DELTA 60 CNC" 2 => "Calandre idrauliche 3 rulli" ... ] ] "links" => array:1 [▼ "url" => array:2 [▼ 0 => array:20 [▶] 1 => array:20 [▶] ] ] ] ]