当前位置: 代码迷 >> 综合 >> laravel8 采集+图片本地化
  详细解决方案

laravel8 采集+图片本地化

热度:20   发布时间:2023-10-20 16:03:38.0

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 "采集失败";}}