1.安装QueryList插件到自己的项目中,一般在vendor文件夹中
[geandeiMac:Html gean$ cd /Volumes/程序开发/www/Html/myapp/
[geandeiMac:Html myapp gean$ composer require jaeger/querylist
执行上面的代码会自动安装插件
2.获取指定网站某div下的数据(以fx168为例获取新闻列表以及每篇文章的content)
namespace app\controller;
use app\BaseController;
use QL\QueryList;
class Demo extends BaseController
{
public function index()
{
$result = [
'status' => 1,
'message' => 'ok',
'code' => 200
];
return json($result);
}
//通过QueryList插件采集新闻内容
public function GetHtmlCode()
{
$url = 'https://news.fx168.com/top/';
$rules = [
// 采集文章链接
'href' => ['.yy_div1>a', 'href'],
// 采集文章标题
'title' => ['.yy_pp2>a', 'text'],
// 采集图片
'img' => ['.yy_div1>a>img', 'slt'],
//采集摘要
'tags' => ['.yy_div2 .yy_pp3>a', 'text'],
//采集时间
'date' => ['.yy_span2', 'text']
];
$range = '#yy_sticker ul li';
$rt = QueryList::get($url)->rules($rules)->range($range)->query()->getData();
$data = $rt->all();
dump($rt);
foreach($data as $k=>$v){
$data[$k] = array_filter($v);
}
$data = array_filter($data);
foreach ($data as $key => $k){
$content_url = $data[$key]['href'];
$c_rules = [
//采集文章内容
'content' => ['.TRS_Editor', 'html'],
];
$c_range = '.text';
$c_rt = QueryList::get($content_url)->rules($c_rules)->range($c_range)->query()->getData();
$content = $c_rt->all();
$data[$key]['content_url'] = $content_url;
$data[$key]['content'] = $content;
}
dump($data);
}
}