<?php
/**
* Created by PhpStorm.
* User: s
* Date: 2018/11/6
* Time: 18:14
*/
include "vendor/autoload.php";
//引入类
use QLQueryList;
class QueClassTwo
{
protected $_pdo;
public function __construct()
{
$this->_pdo = new PDO("mysql:host=127.0.0.1;dbname=student","root","root");
}
public function getDate($url)
{
//设置采集规则
$rules=[
'title'=>['h3','text'],
'url'=>['.juti_new>.juti_list>h3>a','href'],
'img'=>['.ju_pic>a>img','src'],
'content'=>['.juti_new>.juti_list>h3>a','href'],
'addtime'=>['.ping03','text']
];
$data = QueryList::Query($url,$rules)->data;
$this->addData($data);
return true;
}
//数据入库
public function addData($data)
{
if(empty($data)) return false;
$sql="insert into seven_17(title,url,img,content,addtime) VALUES ";
foreach ($data as $k=>$v)
{
if(!empty($v['img']))
{
//名称按/分割后数组
$imgname=explode('/',$v['img']);
//根据数量最后一位获取图片名称
$num=count($imgname);
$name=$imgname[$num-1];
//写入
$imgdata=file_get_contents($v['img']);
file_put_contents('./img/'.$name,$imgdata);
$sql .="('{$v['title']}','{$v['url']}','{$v['img']}','{$v['content']}','{$v['addtime']}'),";
}
}
$sql = substr($sql,0,-1);//除去sql语句后面的,号
$this->_pdo->exec($sql);
return true;
}
}