QueryList采集

代码:

<?php
   //header头防乱码
header("content-type:text/html;charset=utf8");
//引入文件
require 'QueryList/phpQuery-single-master/phpQuery.php';
require 'QueryList/QueryList-3.2.1/QueryList.php';
//命名空间
use QL\QueryList;

//采集
$data = QueryList::Query('采集的网址',array(
'字段名' => array('jQuery选择器','内容')
)) -> data;
//打印
   //print_r($data);die;
//图片保存本地(要判断所采集的图片是否是绝对路径,如果是可以直接使用$v['img'],如果不是就要在获取的路径前加上绝对路径)
foreach ($data as $k => $v) {
$img = file_get_contents($v['img']);
$a = pathinfo($v['img']);
file_put_contents($a['basename'],$img);
};
//连接数据库
$db = new PDO('mysql:host=localhost;dbname=数据库','用户名','密码',array(PDO::ATTR_PERSISTENT));
$db -> query("set names utf-8");
//添加
foreach ($data as $v){
$res = $db -> prepare("insert into 表名 values (null,:字段名)");
$res -> execute($v);
}
if ($res){
echo "入库成功";
     //在两秒后跳转到show.php
header('Refresh:2,Url=show.php');
}else{
echo "入库失败";
}

猜你喜欢

转载自www.cnblogs.com/wanglongfei/p/9806246.html