1 <?php 2 header("content-type:text/html;charset=GBK"); 3 class CurlClass 4 { 5 protected $_pdo; 6 public function __construct() 7 { 8 $this->_pdo =new PDO("mysql:host=127.0.0.1;dbname=student","root","root"); 9 $this->_pdo->query("set names gbk"); 10 } 11 //curl 12 public function get($url) 13 { 14 $ch = curl_init(); 15 curl_setopt($ch, CURLOPT_URL, "$url"); 16 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 17 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); 18 curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,10); 19 curl_setopt($ch, CURLOPT_HEADER, 0); 20 $output = curl_exec($ch); 21 curl_close($ch); 22 return $output; 23 } 24 //规则 25 public function name($url) 26 { 27 $data = $this->get($url); 28 $zheng_ze='#<li>s*<a href="(.*?)".*?>(.*?)<.*s*<span>.*?</span>s*</li>#'; 29 preg_match_all($zheng_ze,$data,$arr); 30 $arr=array_slice($arr,1); 31 foreach($arr[0] as $k=>$v) 32 { 33 $ww=[ 34 'title'=>$arr[1][$k], 35 'content'=>$arr[0][$k] 36 ]; 37 $sql=$this->_pdo->exec("insert into user VALUES (null,'{$ww['title']}','{$ww['content']}')"); 38 } 39 if($sql) 40 { 41 echo "添加成功"; 42 } 43 44 } 45 46 }