码迷,mamicode.com
首页 > 其他好文 > 详细

QueryList采集数据并保存图片到本地

时间:2021-01-04 10:33:29      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:news   http   col   new   文件   固定   style   rand   nbsp   

 1 // 抓取,添加数据
 2     public function save(){
 3         require ‘QueryList/phpQuery.php‘;
 4         require ‘QueryList/QueryList.php‘;
 5          // 要抓的网址
 6         $url = ‘http://www.techweb.com.cn/newgame‘;
 7         $rules =
 8          [
 9             ‘title‘ => [‘.news_title>h3>a‘,‘html‘],
10             ‘text‘ => [‘.news_text>p‘,‘html‘],
11             ‘img‘ => [‘.na_pic>img‘,‘src‘]
12          ];
13         $data = @QueryList::Query($url,$rules)->data;
14         // 将数据中的图片循环
15         foreach ($data as $key=>$val){
16             $res = file_get_contents($val[‘img‘]);
17             // 固定保存到指定文件夹中
18             $info = ‘img/‘.time().rand(1,999999).substr($val[‘img‘],strrpos($val[‘img‘],‘.‘));
19             file_put_contents($info,$res);
20         }
21          // 用Db方法把抓取数据存到数据表“query”中
22         Db::table(‘query‘)->insertAll($data,true);
23     }

 

QueryList采集数据并保存图片到本地

标签:news   http   col   new   文件   固定   style   rand   nbsp   

原文地址:https://www.cnblogs.com/SlytherinGirl/p/14209717.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!