阿狸表情图采集代码示例
百度网盘: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"); ?>
起点在哪,或许选择不了。重要的是,你追求的终点在哪!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· DeepSeek+PageAssist实现本地大模型联网
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· 从 14 秒到 1 秒:MySQL DDL 性能优化实战