爬虫脚本 php,SpiderX

本文详细介绍了PHP爬虫脚本框架SpiderX的使用方法,包括配置、规则设置、无限层级采集、队列去重、调试模式等功能。通过示例代码展示了如何进行网页数据的抓取和处理,支持正则、XPath等多种数据提取方式,适用于各种复杂的网站数据采集需求。
摘要由CSDN通过智能技术生成

php爬虫脚本

框架只做分发,不做数据处理,需要自己在回调中定制。

不限制采集方式,可以用正则,Xpath,字符串截取。

无限层级采集,可以实现列表->详情,列表->列表->详情,详情->详情等任意姿势采集。

队列去重,可以增量抓取,也可以全量采集。

支持调试模式,实时报表,守护模式。

安装依赖

环境

说明

php

>5.6,最好是php7以上

redis

数据队列

快速开始

1、复制代码到index.php文件中

if (!is_file('./vendor/autoload.php')) {

exec("composer require yangze/spiderx");

}

include_once __DIR__ . '/vendor/autoload.php';

$config = [

'name' => 'sina',

'tasknum' => 1,

'start' => [

'http://roll.news.sina.com.cn/news/gnxw/gdxw1/index.shtml',

],

'rule' => [

[

'name' => 'list',

'type' => 'list',

'url' => '#gdxw1/index_\d+.shtml#',

'data' => [

'title' => function ($pageInfo, $html, $data) {

preg_match_all('/

(.*?)/i', $html, $matches);

return $matches[1];

}

]

],

]

];

$spider = new SpiderX\SpiderX($config);

$spider->on_fetch_list = function ($pageInfo, $html, $data) {

file_put_contents(__DIR__ . '/data.txt', implode("\n", $data['title']) . "\n", FILE_APPEND | LOCK_EX);

};

$spider->start();

2、命令行执行(需要composer下载依赖,时间跟网速有关)

php index.php run

配置说明

字段

类型

说明

name

string

任务名称,队列名称根据name值生成。如果要做分布式的,可以选择用相同的name值

tasknum

int

任务数量,默认为1

start

array

采集入口url

rule

array

采集规则,具体参考下方说明

rule值说明

rule值为数组形式,每个二级元素为一个单元。

字段

类型

说明

name

string

任务名称,队列名称根据name值生成。如果要做分布式的,可以选择用相同的name值

name

string

页面类型,选项为list 或者detail

url

string

入口url,有2种形式,一种是'#article_\d+#'这样的正则,从各个页面中抓取;一种是取其他单元的name值与所取单元的data值组合,比如一个单元name为news_list,data中有个元素为url,则组合成news_list.url赋值给当前字段

data

array

要采集的数据,以回调方式赋值,形式为:key => function ($pageInfo, $html, $data) { return '';}

回调说明

通用回调方法

开始任务

on_start = function() use($spiderx) {

// 可以在此方法中添加用户登录,增加url队列操作

//$spiderx->addUrl([]);

}

任务完成

on_finish = function() {

// 任务执行完成,可以发送通知,导入数据库,删除日志文件等

}

向队列中添加url数据

on_add_url = function($pageInfo) {

// 如果调转当前回调,需要返回true,才会向队列中添加数据

}

重试,url请求失败,重新请求,默认为3次

on_retry_page = function($pageInfo) {

//返回true表示需要重试

}

如果获取不到html数据,可以重写setGetHtml方法

setGetHtml = function($pageInfo) {

return file_get_contents($pageInfo['url']);

}

类似的还有setGetLinks方法,抽取页面中的链接,或者其他url存储方式

setGetLinks = function($html) {

}

页面加载回调

需要依赖用户设置的每个rule下面单元的name值。假设我们设置的name值为news. 则对应的回调方法有:

请求url前回调

on_loadding_{news,需要替换不同的name值} = function($pageInfo) {

// pageInfo 为当前页面的相关信息

//返回true表示需要请求这个页面

}

获取html后回调

on_loaded_{news,需要替换不同的name值} = function($pageInfo, $html) {

//html表示当前的html数据

}

解析页面数据后回调,一般用于保存数据

on_fetch_{news,需要替换不同的name值} = function($pageInfo, $html, $data) {

//data值为解析的数据

}

高级玩法

模拟登录

需要要on_start回调中添加自动登录逻辑

部分页面可能需要先get方式获取页面中的参数,然后再发起POST请求

$spider->on_start = function () use ($spider) {

$pageInfo = [

'type' => 'index',

'name' => 'login',

'method' => 'post', // 发送post提交

'url' => 'http://127.0.0.1:3200/index.php/action/login?_=b0fd8734e0687a6cfe352e3f0fcbc5f6',

'query' => [

'name' => 'admin',

'password' => 'admin',

'referer' => 'http://127.0.0.1:3200/admin/',

], // 请求参数

'cookie' => true, // 需要共享cookie

'extra' => [

'headers' => [

'User-Agent' => 'testing/1.0',

'Accept' => 'application/json',

]

]// 添加额外参数,参考

];

$spider->addUrl($pageInfo);

return true;

};

无限级数据采集

实现的方式就是在data的单元中,把url的值设置为上一个单元的name.DataField的形式

参考demo目录sina文件。

post提交表单,post分页抓取数据

实现方式为自定义添加url队列,请求类型method为post,请求参数query为数组或者字符串形式:

$spider->addUrl([

'type' => 'detail', // 保持和单元的name,type一致

'name' => 'detail',

'url' => 'http://smeimdf.mofcom.gov.cn/news/searchEntpAudit.jsp',

'method' => 'post', // 请求方式

'query' => [ // 请求参数

'fund_type' => $fund_type,

'province' => 340000,

],

'context' => [ // 上下文数据,可以很方便的在多任务中传数据

'fund_type' => $fund_type,

'province' => '-',

'province_name' => '-',

]

]);

快速导出列表数据和表格数据

$dataList = (new \SpiderX\Lib\UtilXpath)->setAttr(['title'])->setHtml($html)->setRange('//table[@id="YKTabCon2_10"]')->getResult();

foreach($dataList as $data) {

array_walk($data, function (&$item) {

$item = str_ireplace(',', ',', $item);

$item = trim($item);

});

file_put_contents('data.csv', implode(',', $data) . "\n", FILE_APPEND | LOCK_EX);

}

执行效果

Usage:

demo/sina.php {command} [--opt -v -h ...] [arg0 arg1 arg2=value2 ...]

Options:

--debug Setting the application runtime debug level

--profile Display timing and memory usage information

--no-color Disable color/ANSI formessage output

-h, --help Display this helpmessage

-V, --version Show application version information

Internal Commands:

helpShow application helpinformation

list List all group and alone commands

version Show application version information

Available Commands:

- Alone Commands

daemon Run script indaemon modal

debug Run script indebug modal

run Run script with report

status Show the SpiderX status

stop Stop all the spiderX Process

More commandinformation, please use: demo/sina.php {command} -h

代码参考

参考demo目录

效果参考

命令行

command.jpg?raw=true

报表模式

a92471f09c5721ca06553a6a16e8d137.png

守护模式:

运行后看不到,不截图了。

后续功能

脚手架,自动生成代码

支持深度优先和广度优先

命令行效果

异步多线程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值