//爬虫网站路由
Route::get('/querylist/list','querylistControllers@querylist');
//控制器
<?php
namespace App\Http\Controllers;
use App\models\querylistModel;
use Illuminate\Http\Request;
use QL\QueryList;
class querylistControllers extends Controller
{
//
public function querylist()
{
//1,要爬虫的网站
$url = "http://cms.querylist.cc/news/566.html";
//2获取网页的内容
$content = file_get_contents($url);
// var_dump($content);
//采集规则
$rules = [
'title' => ['h1', 'text'], //文章的标题
'time'=>['span','text'],//文章的时间
];
//采集的范围
$rang = '.post_title';
//采集的数据结果
$result = QueryList::get($url)
->rules($rules)
->range($rang)->query()
->getData()
->all();
// var_dump($result);
$success=querylistModel::querylist($result);
if ($success){
echo '采集成功';
}else{
echo '采集失败';
}
}
}
//////////////////////////////模型
class querylistModel extends Model
{
//
protected $table='querylist';
public $primaryKey='id';
public $timestamps=false;
public static function querylist($params){
return self::insert($params);
}
}