当获取一份不规则excel如图
如何将数据快速入数据库呢?
首先php原生方式肯定可以,但不是本次重点,因为swoole协程的魅力,我已经爱上了hyperf爬去数据,所以根据之前hyperf爬取页面经验,我想到可以将excel先转换为一个页面,之后再爬虫页面数据,从而将大批量获取excel数据入库。
hyperf项目 app\Controller下新建 PansController.php
PansController.php
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Controller;
use QL\QueryList;
use Hyperf\DbConnection\Db;
class PansController extends AbstractController
{
public function ziyuan(){
// 元数据采集规则
$rules = [
'pkey' => ['td:eq(0)','text'],
'title' => ['td:eq(1)','text'],
'urlpath' => ['td:eq(2)','text'],
];
// 切片选择器
// $range = '.content li';
$range = 'body tr';
$rt = QueryList::get("http://look.zlhdsg.com/ziyuan.html")->rules($rules)->range($range)->query()->getData();
// var_dump($rt);
foreach ($rt as $k=> $r){
if(!empty($r['pkey']&&!empty($r['title'])&&!empty($r['urlpath']))){
$res=Db::table("pans")->insert($r);
}
}
}
public function ziyuan2(){
// 元数据采集规则
$rules = [
'ma' => ['td:eq(0)','text'],
'title' => ['td:eq(1)','text'],
'urlpath' => ['td:eq(2)','text'],
'extra' => ['td:eq(3)','text'],
];
// 切片选择器
// $range = '.content li';
$range = 'body tr';
$rt = QueryList::get("http://look.zlhdsg.com/ziyuan2.html")->rules($rules)->range($range)->query()->getData();
var_dump($rt);
foreach ($rt as $k=> $r){
if(!empty($r['ma']&&!empty($r['title'])&&!empty($r['urlpath']))){
$res=Db::table("games")->insert($r);
}
}
}
}
http://look.zlhdsg.com/ziyuan.html 与 http://look.zlhdsg.com/ziyuan2.html 是两个excel转换成的页面,厉害吧狠起来连自己都爬。
衍生品:
qwe
qwe
qwe
asd
a
阿斯达