Class MyCurl{ protected $_pdo; //构造方法 链接数据库 public function __construct(){ $this->_pdo=new PDO("mysql:host=localhost;dbname=baseinfo","root","root"); } //CURL抓取 public function get($curl){ $ch=curl_init($curl); curl_setopt($ch,CURLOPT_URL,$curl); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_HEADER,0); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,10); $data=curl_exec($ch); curl_close($ch); return $data; } public function show($curl){ $a=$this->get($curl); $b='#(正则表达式)#'; preg_match_all($b,$a,$arr); (获取新闻信息获取数据,foreach循环处理) foreach($arr[0] as $k =>$v){ $arr1[]=[ 'url'=>$arr[1][$k], 'name'=>$arr[2][$k], 'money'=>$arr[3][$k], 'address'=>$arr[4][$k], 'company'=>$arr[5][$k], 'time'=>$arr[6][$k] ]; } $this->add($arr1); } //添加数据 public function add($arr1){ foreach($arr1 as $kk =>$vv){ $url=$vv['url']; $name=$vv['name']; $company=$vv['company']; $money=$vv['money']; $time=$vv['time']; $address=$vv['address']; $sql="insert into yuekao values(null,'$name','$company','$money','$time','$address','$url')"; $aa=$this->_pdo->query($sql); if($aa==1){ header("location:show.php");//跳转展示页面 } } } } }
//展示数据
<?php $pdo=new PDO("mysql:host=localhost;dbname=baseinfo","root","root");//PDO连接数据库 $sql="select * from yuekao"; $list=$pdo->query($sql)->fetchAll(); include "show.html";
show.html展示数据