IT学习联盟

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
IT学习联盟 门户 文章 C/C++/VC++/MFC/QT 查看内容

用CI框架写的抓取文章标题、内容、来源图片代码

2012-8-18 02:46| 发布者: admin| 查看: 879| 评论: 0

整理了一下,之前的有些乱看不清楚,这是我其中一个抓取页面的代码,做一个备份,方便以后调用,本人能力有限希望大家多给点意见

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('/<strong>\d\/(.*?)<\/strong>/', $result, $page_news);
       //print_r($page_news);
       //echo '<hr>';
       /*获取单个类别下所有分页页面的新闻列表*/
       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('/<td width="516" height="28" class="z14"><a href="(.*?)" target="_blank">.*?<\/a><\/td>/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('/<h3 class="title"><strong>(.*?)<\/strong><\/h3>/sim', $result_newsinfo, $title);
                 //print_r($title[1]);
                 /*获取来源*/
                 preg_match_all('/<td style="BORDER-BOTTOM: #666666 1PX DASHED" width="155"><span class="right">【来源:(.*?)&nbsp;】<\/span><\/td>/sim', $result_newsinfo, $source);
                 //print_r($source[1]);
                 /*获取内容*/
                 preg_match_all('/<td colspan="2" class="z14" style="padding-top:20px;padding-left:1px;padding-bottom:20px;line-height:25px">(.*?)<\/td>/sim', $result_newsinfo, $content);
                 //print_r($content[1][0]);
                 /*获取内容里的所有图片url*/
                 //preg_match_all('/<IMG alt="" src="(.*?)">/sim', $content[1][0], $img);
                 preg_match_all('/<IMG.*?src="(.*?)".*?>/sim', $content[1][0], $img);
                 //echo 'ddddd';
                 //print_r($img[1]);
                 //echo 'dddd<br>';
                 //exit;
                 $picture = '';
                 foreach($img[1] as $imgs)
                 {
                      //echo $imgs;
                      //echo '<br>';
                      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<br>';
                 //print_r($content[1][0]);
    
                 //echo '<br>';
                 $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 '<hr>';
   
       }
  }
$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); 
}


Archiver|IT学习联盟| 网站地图

Powered by Discuz! X2.5© 2001-2010 世界学习室 大伟制作

GMT+8, 2018-10-15 21:31 , Processed in 0.087314 second(s), 12 queries .