阿狸表情图采集代码示例

百度网盘:http://pan.baidu.com/s/1pJz7EER

http://blog.jjonline.cn/phptech/175.html(转)

代码:

    <?PHP
    header("content-type:text/html;charset=utf-8");
    /**Base Function***/
     // exit;
    /**
    * get远程文档
    * @access public
    * @param string $url 远程url
    * @return mixed
    */
    function GetUrl($url) {
        if (function_exists('file_get_contents')){
            return file_get_contents($url);
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_URL, $url);
        $result =  curl_exec($ch);
        curl_close($ch);
        return $result;
    }
     
    /**
    * 采集远程文件
    * @access public
    * @param string $remote 远程文件名
    * @param string $local 本地保存文件名
    * @return mixed
    */
    function curlDownload($remote,$local) {
        $cp = curl_init($remote);
        $fp = fopen($local,"w");
        curl_setopt($cp, CURLOPT_FILE, $fp);
        curl_setopt($cp, CURLOPT_HEADER, 0);
        curl_exec($cp);
        curl_close($cp);
        fclose($fp);
    }
     
    /**采集代码开始干活的搞起**/
    $BaseUrl         =     'http://www.a-li.com.cn/';
    $EmotionUrl     =     $BaseUrl.'download/qq_emotion/index.php?page=';
     
    //采集页面的开始与结束
    $BeginPage     =     1;
    $EndPage        =    37;//总页面数;阿狸官方站更新后 总页面数可能会变化 在此处配置
     
    //$_SESSION['page'] = null;exit;//不注释该项重新初始化
     
    //采用session记录采集到哪个页面
    if(!$_SESSION['page']) {
        $Page = $BeginPage;
        $_SESSION['page'] = $Page;
    }else {
        $Page = $_SESSION['page'] + 1;
        $_SESSION['page'] = $Page;
    }
    if($Page>$EndPage) {exit('活已干完!');}
     
    //开始采集
    $url = $EmotionUrl.$Page;
    $string = GetUrl($url);
    while(!$string) { //while 循环保证采集无误
        sleep(3);
        $string = GetUrl($url);
    }
    //正则匹配采集到的页面中的阿狸gif图片信息
    $match  =    array();
    $string = preg_replace('/<!--.*s*.*-->/i','',$string);//去除html里的注释段  可以删掉
    preg_match_all('/<img src="(data/attachment.*.gif)"s+alt="(.*)"s+.*s+/>/i',$string,$match,PREG_SET_ORDER);
     
    //采集后的文件存储位置 检测文件夹不存在就创建
    if(!is_dir('./ali/')) {
        mkdir('./ali/');
    }
     
    //检测匹配并循环下载采集到的gif图到与该PHP代码文件同级目录下的ali目录中
    if($match) {
        foreach($match as $key=>$value) {
            $imgUrl = $BaseUrl.$value[1];
            $imgName = './ali/'.preg_replace('/./','',$value[2]).'.gif';//使用匹配到的img标签中的alt作为本地保存gif图的文件名
            curlDownload($imgUrl,$imgName);
        }
    }else {
        exit('匹配信息出错');
    }
    echo '<p>采集第'.$Page.'页中的阿狸gif图片完成。</p>';
    echo '<p>1秒后继续自动执行下一个页面</p>';
    echo '<script>setTimeout(function () {window.location.reload(true);},1000);</script>';
    ?>

几点注意:应当注意2个函数

GetUrl($url)  与  curlDownload($remote,$local)

代码一:

<?php
function curlDownload($remote,$local) {
        $cp = curl_init($remote);
        $fp = fopen($local,"w");
        curl_setopt($cp, CURLOPT_FILE, $fp);
        curl_setopt($cp, CURLOPT_HEADER, 0);
        curl_exec($cp);
        curl_close($cp);
        fclose($fp);
    }

    curlDownload('http://blog.jjonline.cn/Upload/image/201408/20140810151040.gif','aa.gif');

?>

代码二:

<?php
 function GetUrl($url) {
        if (function_exists('file_get_contents')){
            return file_get_contents($url);
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_URL, $url);
        $result =  curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    echo GetUrl("http://blog.jjonline.cn/phptech/175.html");
?>
原文地址:https://www.cnblogs.com/wuheng1991/p/5235749.html