1.使用QueryList
//采集public function collect(){
$html=file_get_contents('https://n.eastday.com/pnews/162337384677010918');$rules=['title'=>['h1','text'],'author'=>['.author','text'],'img'=>['img','src'],'talk'=>['p','text']];$data=QueryList::html($html)->rules($rules)->range('.mainBox')->query()->getData();$result=$data->all();foreach ($result as $k=>$v){
$name=file_get_contents($v['img']);$filename="./uploads/1".$k.".jpg";file_put_contents($filename,$name);$filename=substr($filename,1);}$data=Messmodel::add($result);if ($data){
echo "采集并添加入库成功";}else{
echo "采集失败";}}