求助php+mysql采集程序源码。主要采集文章和图片,但是在文章里包括图片。这功能怎么实现的。

解决方案 »

  1.   

    我给你思路 源码就算了!
    1.获取某篇文章的url
    2.获取文章的html源码  $content = file_get_contents($ulr);
    3.通过正则表达式 先匹配出来文章内容
    4.在文章内容里面通过正则表达式匹配出来img
    5.img通过file_get_contents函数下载下来6.讲上述的内容保存起来 那么这篇文章就采集结束!
      

  2.   

    源码就没有了,思路还是有的。
    curl/get_contents + reg 就行了,至于附件图片,可以根据img的src用curl下载到本地,再根据本地路径进行路径的替换,ok
      

  3.   


    require_once("snoopy.class.php");
    $img_array = array(); 
    $img_path = realpath("img").'/'.date("Y-m-d").'/'; //采集远程图片保存地址
    $url = 'http://www.bokenya.jp/idolbb/3rd.html?MSTID=ARKX-008'; //你要采集的页面
    $snoopy = new Snoopy;
    $snoopy->fetch($url);$body = $snoopy->results;
    //preg_match_all("/(src|src)=[\"|\'|]{0,}(http:\/\/(.*)\.(jpg|jpeg|png))/isu",$body,$img_array);//采集网页图片的正则
    preg_match_all("/http:\/\/.*\.jpg/is",$body,$img_array);
    $img_array = array_unique($img_array[0]);//array_unique() 接受 array 作为输入并返回没有重复值的新数组。
    //print_r($img_array);
    foreach($img_array as $key => $value){
      $filetime = time();
      $filename = date("ymdhis",$filetime).rand(1,999).'.'.substr($value,-3,3); 
      $snoopy->fetch($value);
      $get_file = $snoopy->results;
      echo "<img src=".$value."><br>\n";
      if( mkdirs($img_path) )
       {
        $fp = fopen($img_path.$filename,"w");
        fwrite($fp,$get_file);
        fclose($fp);
        //@sleep(6);
       }  
    }function mkdirs($dir)
    {
     if(!is_dir($dir)){
      if(!mkdirs(dirname($dir))){
       return false;}
      if(!mkdir($dir,0777)){
       return false;}
     }
     return true;
    } 这个是我以前的,需要在本目录下建立一个“temp”文件夹以存储下载来的图片, 还需要Snoopy类的支持,网上多的是
      

  4.   

    1.获取某篇文章的url
    2.获取文章的html源码 $content = file_get_contents($ulr);
    3.通过正则表达式 先匹配出来文章内容
    4.在文章内容里面通过正则表达式匹配出来img
    5.img通过file_get_contents函数下载下来
      

  5.   

    dedeCMS后台就有采集功能,你下载个研究下就是了!
      

  6.   

    光喊有啥用!?上面的很多代码都是能用的,原理你也看到了,再者dedeCMS有很多版本的utf-8、gbk的都有你下载个看看。
      

  7.   

    <?php
    /*
     * Created on 2010-12-12
     *
     * To change the template for this generated file go to
     * Window - Preferences - PHPeclipse - PHP - Code Templates
     */
     include 'include/common.php';$sql = 'select url from wb_url';
    $result = $conn->query($sql);if($result && $conn->affected_rows){
    //echo 'sdf';
    while($row = $result->fetch_object()){
    // var_dump($row->url);
    // echo 'aaaaaaaaaaa';
    //开始设置抓取步骤
    //echo $url = $row->url;
    $url = $row->url;
    $file = fopen($url,"r");
    //打开具体地址 r 代表 以只读方式打开
    if(!$file){
    echo '打开失败';
    exit;
    }
    if(!feof ($file)){ $line = fread($file,1024);
    $pattern = "|<title>(.*)<\/title>|ims";if (preg_match($pattern, $line, $out)) { //var_dump($out);
    //echo 'gad';
    //echo "".$out[1].""."</br>";
    echo '<a href=$url>'.$out[1].'</a>'.'<br>';
    //匹配文章中有链接
    //如果成功  就将从指定网站的抓取的数据 写入数据库
    $pattern1 = "/(window[.]location)[[:blank:]]*=[[:blank:]]*[\'\"]?(([[a-z]{3,5}:\/\/(([.a-zA-Z0-9-])+(:[0-9]+)*))*([+:%\/?=&;\\\(\),._ a-zA-Z0-9-]*))(#[.a-zA-Z0-9-]*)?[\'\" ]?/i";
    if (preg_match($pattern1, $line, $out)){
    var_dump($out);
    } fclose($file); // break;
    }}//!feof($file)
    }//while
    }//if
    $conn->close();
    ?>
      

  8.   

    1.获取某篇文章的url2.获取文章的html源码 $content = file_get_contents($ulr);
    3.通过正则表达式 先匹配出来文章内容
    4.在文章内容里面通过正则表达式匹配出来img
    5.img通过file_get_contents函数下载下来6.讲上述的内容保存起来 那么这篇文章就采集结束!
      

  9.   

    之前做过个获取淘宝商品信息的:
      调用淘宝的数据当然可以使用淘宝提供的api啦,只是基于当前项目只需调用淘宝商品图片名称等公开信息在自己网站上就ok,因此使用php中的file_get_contents函数实现。思路:
    file_get_contents(url)该函数根据url如http://www.baidu.com将该网页内容(源码)以字符串形式输出(一个整字符串),然后配合preg_match,preg_replace等这些正则表达式操作就可以实现获取该url特定div,img等信息了。当然前题是淘宝在单个商品页面的结构是固定的,如500图的img中id就是J_ImgBooth,我想在一段时间内这种命名方式应该不会变吧!具体实现:(获取500图,名称,价格,属性及商品描述)
    $text=file_get_contents("http://item.taobao.com/item.htm?id=2380347279"); //将url地址上页面内容保存进$text
    A.获取500图:
    preg_match('/<img[^>]*id="J_ImgBooth"[^r]*rc=\"([^"]*)\"[^>]*>/', $text, $img);
    //运用正则抓取img标签中id为J_ImgBooth的img,$img[0]为该500图img标签,$img[1]为500图的图片地址;
    B. 获取名称:
    preg_match('/<title>([^<>]*)<\/title>/', $text, $title);
    //因为正文中的商品名称标签没有特殊class或id正则不好抓取,就抓<title>标签中的内容了,一般来说title中内容就是商品名称了(实际有些出入),$title[0]整个title标签 $title[1]标签中内容;
    $title=iconv('GBK','UTF-8',$title);
    //如果你的网站是utf8编码,那么需要进行一下转码(淘宝是gbk编码)
    C.获取价格:
    preg_match('/<([a-z]+)[^i]*id=\"J_StrPrice\"[^>]*>([^<]*)<\/\\1>/is', $text, $price);
    //同理获取id为J_StrPrice的标签内容$price[2], $price[0]是整个标签, $price[1]为strong标签名;
    $price=floatval($price);//放入数据库估计还有转一下变量类型
    D.获取属性:
    这之前获取的内容都是在单标签中相对只需一个正则就可搞定,然而如果要获取如

    <div id=”xxx”>

    <ul>

    </ul>
    <div>…
    <div>…
    </div>
    </div>
    </div>

    这样特定div中有未知n个<>标签,获取该特定div将会非常的困难,搜了下网上,最接近的也只是"/<([a-z]+)[^>]*>([^<>]|(?R))*<\/\\1>/"这样使用递归抓取标签对,但是他不能抓特定标签,所以想要轻松抓取class="attributes"的div我是没法办到了。但是淘宝网页有其特殊性,就是它的各个标签结构基本是固定的…<div class="attributes">…</div>标签后面不是</div><div id="description">就是</div><div class="box J_TBox">,所以我们可以采用变通法达到获取属性标签内容的目的。preg_match('/<(div)[^c]*class=\"attributes\"[^>]*>.*<\/\\1>/is', $text, $text0);
    //这个正则会抓取<div class="attributes"开始到整个页面最后一个</div>标签,当然我们属性标签就在这个的前面部分。
      $text1=preg_replace("/<\/div>[^<]*<(div)[^c]*id=\"description\"[^>]*>.*<\/\\1>/is","",$text0);
    //匹配到</div ><div id="description">至最后</div>然后用””代替(就是把匹配的删除了),所以如果attributes的div后面紧跟的是description那么我们已经达到目的了。 $attributes=preg_replace("/<\/div>[^<]*<(div)[^c]*class=\"box J_TBox\"[^>]*>.*<\/\\1>/is","",$text1);
    //如果attributes后面紧跟box J_Tbox标签,那么我们还需要使用以上这步来剔除box J_Tbox标签,当然如果attributes的div后面紧跟的是description,这一步将不会匹配到任何即什么都不会做。E.获取描述:
    通过上面方法你肯定觉得淘宝页面上任何标签都可以很简单获取了吧(我之前也是这么想的),但是使用这个方法获取描述时得到的内容将会是“描述加载中”,是的,这个描述内容不是在源码中的,它是打开页面加载进一大堆js后,不知道从淘宝的哪个角落中加载进来的。
    好吧,那么我们也可以模仿它放一些js进去。不知道哪些对加载描述有用?没事,全加载进来肯定没错。不知道需要放那些特定div上去有作用?抓一个源码,删掉一些div一步步试试看,你会发现“<div id="detail" class="box"> </div>
      <div id="description" class="J_DetailSection">
      <div class="content" id="J_DivItemDesc">描述加载中</div>
      </div>”
    这几个div是加载描述所必须的,那么下面就是写代码了:preg_match_all('/<script[^>]*>[^<]*<\/script>/is', $text, $content);//页面js脚本
    $content=$content[0];
    $description='<div id="detail" class="box"> </div>
      <div id="description" class="J_DetailSection">
      <div class="content" id="J_DivItemDesc">描述加载中</div>
      </div>';
    foreach ($content as &$v){$description.=iconv('GBK','UTF-8',$v);};
    //将这个$description放进页面,描述就会自动的加载进来了,当然多个商品描述在同一个页面也会只有一个描述会被加载的。
    下面通过图片地址保存原图+水印+缩略图函数,希望能帮到你:
    function img_show($path,$shop_name,$img_name)
        {
            $resorce = getimagesize($path);
            if ($resorce)
            {
    $Water=M("water");
    $seat=$Water->where("id=1")->find();
                $type = explode('/', $resorce['mime']);
                $creator = 'imagecreatefrom' . $type['1'];
                $img = @$creator($path);
                if (C('WATERMARK_IMG') == true)
                {
                    $replace_res = getimagesize(C('WATERMARK_IMG_PATH'));
    $img_size = getimagesize($path);
                    $replace_type = explode('/', $replace_res['mime']);
                    if ($replace_res)
                    {
                        $replace_cre = 'imagecreatefrom' . $replace_type['1'];
                        $replace_img = @$replace_cre(C('WATERMARK_IMG_PATH'));
                        imagecopy($img, $replace_img,$img_size['0']-$replace_res['0']-$seat["right_x"], $img_size['1']-$replace_res['1']-$seat["bottom_y"], 0,0, $replace_res['0'], $replace_res['1']);
                    }
                } 
    //大小缩略图的生成
      $smallthumbWidth=C('SMALLTHUMB_SIZE');
      $smallthumbHeight=C('SMALLTHUMB_SIZE');
      $bigthumbWidth=C('BIGTHUMB_SIZE');
      $bigthumbHeight=C('BIGTHUMB_SIZE');
    $smallthumb = imagecreatetruecolor($smallthumbWidth, $smallthumbHeight);   
    $bigthumb = imagecreatetruecolor($bigthumbWidth, $bigthumbHeight);  
    imagecopyresampled($smallthumb, $img, 0, 0, 0, 0,  $smallthumbWidth,  $smallthumbHeight, $img_size['0'],$img_size['1']); 
    imagecopyresampled($bigthumb, $img, 0, 0, 0, 0,  $bigthumbWidth,  $bigthumbHeight, $img_size['0'],$img_size['1']); 
    //500图的生成
                if(!file_exists("images/Good/".$shop_name)){mkdir("images/Good/".$shop_name);};//目录检查及创建
    if(!file_exists("images/Good_thumb/Smallthumb/".$shop_name)){mkdir("images/Good_thumb/Smallthumb/".$shop_name);}
    if(!file_exists("images/Good_thumb/Bigthumb/".$shop_name)){mkdir("images/Good_thumb/Bigthumb/".$shop_name);}
                $outtype = 'image' . $type['1'];
    $nowimg="images/Good/".$shop_name."/".$img_name.".".$type['1'];//原图保存路径
    $smallthumb_path="images/Good_thumb/Smallthumb/".$shop_name."/".$img_name.".".$type['1'];//小缩略保存路径
    $bigthumb_path="images/Good_thumb/Bigthumb/".$shop_name."/".$img_name.".".$type['1'];//大缩略保存路径
                $outtype($img,$nowimg,100);
    $outtype($smallthumb,$smallthumb_path,100);
    $outtype($bigthumb,$bigthumb_path,100);
     imagedestroy($img);
     imagedestroy($smallthumb);
     imagedestroy($bigthumb);
     $pathh=preg_replace('/images.*/',"",C('WATERMARK_IMG_PATH'));
     $img_path["nowimg"]=$pathh.$nowimg;//返回各个图片路径的数组
     $img_path["smallthumb_path"]=$pathh.$smallthumb_path;
     $img_path["bigthumb_path"]=$pathh.$bigthumb_path;
    return $img_path;
            }
           
        }
      

  10.   

    楼主不听大家的劝,遗憾!
    告诉你啦。
    1、你想看你的代码问题,请把你的代码发上来。
    2、参考代码请下载一个dedecms,安装一下研究使用
    3、要是为采集文章,就用dedecms+火车头 ,据说超强大,php里就推荐这套组合了。
      

  11.   

    dede,帝国,不都是带采集的吗?