在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:SpiderX开源软件地址:https://gitee.com/zhenyangze/SpiderX开源软件介绍:php爬虫脚本
安装依赖
快速开始
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('/<li><a href=".*?" target="_blank">(.*?)<\/a><span>/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 配置说明
rule值说明rule值为数组形式,每个二级元素为一个单元。
回调说明通用回调方法开始任务 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值为解析的数据} 高级玩法模拟登录需要要
$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的值设置为上一个单元的
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 for message output -h, --help Display this help message -V, --version Show application version informationInternal Commands: help Show application help information list List all group and alone commands version Show application version informationAvailable Commands:- Alone Commands daemon Run script in daemon modal debug Run script in debug modal run Run script with report status Show the SpiderX status stop Stop all the spiderX ProcessMore command information, please use: demo/sina.php {command} -h 代码参考参考demo目录 效果参考命令行报表模式守护模式:运行后看不到,不截图了。 后续功能
|
请发表评论