外贸建站之数据采集常用PHP代码分享

相信很多人有过网站升级的经验,那就一定会想到怎样将旧网站数据搬迁采集到新网站。也有很多搞外贸建站站群推广的,也会有数据采集的需要。

不同网站系统中的数据只能通过采集才能获得了,除非编程搞一个数据接口,两个站数据可以相互交换。

这里分享一下我们开发中用到的代码:

function get_pic($cont,$path){ 
    $pattern_src = '/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/'; 
    $num = preg_match_all($pattern_src, $cont, $match_src); 
    $pic_arr = $match_src[1]; //获得图片数组 
    $spider = new Spider();
    foreach ($pic_arr as $pic_item) { //循环取出每幅图的地址 
        $pic_item=ltrim($pic_item," ");
        if(is_int(strpos($pic_item, 'http'))){
            $arcurl = $pic_item;
        } else {
            $arcurl = "http://www.chinaobd2.com".$pic_item;        
        }
        $saveimgfile=$spider->downloadImage($arcurl,$path);
        $saveimgfile="/upload/".$saveimgfile;
        $cont=str_replace($pic_item,$saveimgfile,$cont);
    }
    $cont=ereg_replace("<a [^>]*>|<\/a>","",$cont);
    return $cont;
}

这段代码可以从采集的内容里面批量提取图片,然后保存到服务器上。非常实用,以上代码是爱的外贸建站亲测,可用。

Function replace_chinaobd2_com_script($cont){
$preg = "/<script[\s\S]*?<\/script>/i";

$newstr = preg_replace($preg,"",$cont,-1);    //第四个参数中-1 是全部替换,如果是3,则表示替换3次

return $newstr;
}

这段代码可以过滤掉采集内容部分的脚本代码

以上代码进爱的外贸建站程序员亲测,如有问题,欢迎留言。会持续分享有用采集代码给大家,共大家一起学习进步

猜你喜欢

转载自www.cnblogs.com/aid12580/p/10426035.html