public function snatch() { set_time_limit(0); $this->benchmark->mark('code_start'); /*获取不同类别的二手车新闻*/ for($i=1;$i<=4;$i++) { $url = 'http://news.2sche.cn/list.asp?stype='.$i; $result = $this->curl_snatch($url); preg_match_all('/d/(.*?)/', $result, $page_news); //print_r($page_news); //echo '
'; /*获取单个类别下所有分页页面的新闻列表*/ for($j=1;$j<=$page_news[1];$j++) { if(1 == $j) { $url_news = 'http://news.2sche.cn/list.asp?stype='.$i; } else { $url_news = 'http://news.2sche.cn/list.asp?page='.$j.'&stype='.$i; } $result_news = $this->curl_snatch($url_news); preg_match_all('/.*? /sim', $result_news, $url_newslist); //print_r($url_newslist); /*遍历列表页每个url*/ foreach($url_newslist[1] as $url_newslists) { $url_newsinfo = 'http://news.2sche.cn/'.$url_newslists; $result_newsinfo = $this->curl_snatch($url_newsinfo); /*获取标题*/ preg_match_all('/(.*?)
/sim', $result_newsinfo, $title); //print_r($title[1]); /*获取来源*/ preg_match_all('/【来源:(.*?) 】 /sim', $result_newsinfo, $source); //print_r($source[1]); /*获取内容*/ preg_match_all('/(.*?) /sim', $result_newsinfo, $content); //print_r($content[1][0]); /*获取内容里的所有图片url*/ //preg_match_all('/ /sim', $content[1][0], $img); preg_match_all('/ /sim', $content[1][0], $img); //echo 'ddddd'; //print_r($img[1]); //echo 'dddd
'; //exit; $picture = ''; foreach($img[1] as $imgs) { //echo $imgs; //echo '
'; if(strpos($imgs, 'http://') === false) { continue; } $img_source = file_get_contents($imgs); /*获取单个图片的名称*/ $img_names = trim(strrchr($imgs,'/'), '/'); //print_r($img_name); //echo $img_names; //exit; $picture .= $img_names.':'; file_put_contents("./static/uploads/news/".$img_names, $img_source); //图片路径替换 $img_path = '/static/uploads/news/'.$img_names; $content[1][0] = str_replace($imgs, $img_path, $content[1][0]); } //print_r($picture) ; //echo 'hhhh
'; //print_r($content[1][0]); //echo '
'; $data = array( 'title' => $title[1][0], 'source' => $source[1][0], 'contents' => trim($content[1][0]), 'picture' => $picture, 'style' => $i, 'create_time' => time(), ); if(!$this->News_model->add($data)) { continue; } //print_r($data);exit; } echo '
'; } } $this->benchmark->mark('code_end'); echo $this->benchmark->elapsed_time('code_start', 'code_end'); }function curl_snatch($url='http://www.2sche.cn/buy.asp') { $url = trim($url); $content = ''; if (extension_loaded('curl')) { $ch = curl_init(); // 2. 设置选项,包括URL curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_HEADER, 0); // 3. 执行并获取HTML文档内容 $output = curl_exec($ch); $content = iconv("GBK", "UTF-8", $output); if ($output === FALSE) { echo "cURL Error: " . curl_error($ch); } //$info = curl_getinfo($ch); //echo '获取'. $info['url'] . '耗时'. $info['total_time'] . '秒'; // 4. 释放curl句柄 curl_close($ch); } else { $content = file_get_contents($url); } return trim($content); }
作者:李佳顺