php 抓取网站图片的简单程序

简介:
PHP下载CSS文件中的图片
Java代码   收藏代码
  1. <?  
  2. function getImagesFromCssFile() {  
  3. //note 设置PHP超时时间  
  4.     set_time_limit(0);  
  5.   
  6. //note 取得样式文件内容  
  7.     $styleFileContent = file_get_contents('images/style.css');  
  8.   
  9. //note 匹配出需要下载的URL地址  
  10.     preg_match_all("/url\(.+?\)/", $styleFileContent, $imagesURLArray);  
  11.   
  12. //note 循环需要下载的地址,逐个下载  
  13.     $imagesURLArray = array_unique($imagesURLArray[0]);  
  14.     foreach ($imagesURLArray as $imagesURL) {  
  15.         $imagesURL = str_ireplace(array("url("")""'",'"'), '', $imagesURL);  //url(" ") url('')  
  16.         if (preg_match('/^http.*/', $imagesURL)) {   //跳过网络图片  
  17.             continue;  
  18.         }  
  19.         file_put_contents(basename($imagesURL), file_get_contents($imagesURL));  
  20.     }  
  21. }  

 

Java代码   收藏代码
  1. <?php  
  2. /*完成网页内容捕获功能*/  
  3. function get_img_url($site_name) {  
  4.     $site_fd = fopen($site_name, "r");  
  5.     $site_content = "";  
  6.     while (!feof($site_fd)) {  
  7.         $site_content .= fread($site_fd, 1024);  
  8.     }  
  9.     /*利用正则表达式得到图片链接*/  
  10.     $reg_tag = '/<img.*?\"([^\"]*(jpg|bmp|jpeg|gif)).*?>/';  
  11.     $ret = preg_match_all($reg_tag, $site_content, $match_result);  
  12.     fclose($site_fd);  
  13.     return $match_result[1];  
  14. }  
  15.   
  16. /* 对图片链接进行修正 */  
  17. function revise_site($site_list, $base_site) {  
  18.     foreach ($site_list as $site_item) {  
  19.         if (preg_match('/^http/', $site_item)) {  
  20.             $return_list[] = $site_item;  
  21.         } else {  
  22.             $return_list[] = $base_site . "/" . $site_item;  
  23.         }  
  24.     }  
  25.     return $return_list;  
  26. }  
  27.   
  28. /*得到图片名字,并将其保存在指定位置*/  
  29. function get_pic_file($pic_url_array, $pos) {  
  30.     $reg_tag = '/.*\/(.*?)$/';  
  31.     $count = 0;  
  32.     foreach ($pic_url_array as $pic_item) {  
  33.         $ret = preg_match_all($reg_tag, $pic_item, $t_pic_name);  
  34.         $pic_name = $pos . $t_pic_name[1][0];  
  35.         $pic_url = $pic_item;  
  36.         print("Downloading " . $pic_url . "\n");  
  37.         $img_read_fd = fopen($pic_url, "r");  
  38.         $img_write_fd = fopen($pic_name, "w");  
  39.         $img_content = "";  
  40.         while (!feof($img_read_fd)) {  
  41.             $img_content .= fread($img_read_fd, 1024);  
  42.   
  43.         }  
  44.         fwrite($img_write_fd, $img_content);  
  45.         fclose($img_read_fd);  
  46.         fclose($img_write_fd);  
  47.         print("[OK]\n");  
  48.     }  
  49.     return 0;  
  50. }  
  51.   
  52. function main() {  
  53.     /* 待抓取图片的网页地址 */  
  54.     $site_name = "http://image.cn.yahoo.com";  
  55.     $img_url = get_img_url($site_name);  
  56.     $img_url_revised = revise_site($img_url, $site_name);  
  57.     $img_url_unique = array_unique($img_url_revised); //unique array  
  58.     get_pic_file($img_url_unique, "./");  
  59. }  
  60.   
  61. main();  
  62. ?>  

此程序略有不足,如果图片在网站服务器上不同目次下但文件名是相同的,此时图片有可能是不一样的,但在最后生存时,后面得到的图片会将前边已生存的图片覆 盖掉,如在http://example.com/网站上有图片链接http://example.com/pic/test1.jpg和http: //example.com/pic/new/test1.jpg那么在下载时这两张图片只有一张生存,另外一张就被覆盖掉,修正的方法是在每派生的存前 先检索当前目次下是否已有此文件名,有的话对将派生的存的图片从头命名即可。

Java代码   收藏代码
  1. <?php  
  2. //取得页面所有的图片地址  
  3.   
  4. function getimages($str){  
  5.   
  6.     $match_str = "/((http://)+([^ rn()^$!`"'|[]{}<>]*)((.gif)|(.jpg)|(.bmp)|(.png)|(.GIF)|(.JPG)|(.PNG)|(.BMP)))/";  
  7.   
  8.     preg_match_all ($match_str,$str,$out,PREG_PATTERN_ORDER);  
  9.   
  10.     return $out;  
  11.   
  12. }?>  
相关文章
|
3月前
|
PHP
使用PHP实现随机调用图片
使用PHP实现随机调用图片
41 0
使用PHP实现随机调用图片
|
3月前
|
程序员 网络安全 PHP
IIS使用PHPManager发布PHP网站
PHPManager是一款用于IIS(Internet Information Services)的工具,旨在简化在Windows服务器上发布PHP网站的过程。通过PHPManager,用户可以轻松管理PHP版本、配置PHP设置以及进行PHP应用程序的部署。这个工具提供了直观的用户界面,使用户能够更方便地与IIS服务器进行交互,而不需要深入了解服务器配置的技术细节。PHPManager的使用使得在IIS环境中托管和维护PHP网站变得更加便捷,提高了网站的部署效率和管理灵活性。
34 0
|
3月前
|
网络安全 PHP 开发者
IIS服务器发布PHP网站
IIS服务器,相信开发者都不会陌生,它的英文全称是Internet Information Services,是由微软公司提供的基于运行Microsoft Windows的互联网基本服务,常用于Windows系统的Web项目部署,本篇以PHP项目为例,讲解如何使用IIS完成PHP项目的发布。
56 0
|
3月前
|
JavaScript 数据可视化 IDE
分享21个广告排行、15个交友会员、25个网站导航和39个文件管理PHP源码,总有一款适合你
分享21个广告排行、15个交友会员、25个网站导航和39个文件管理PHP源码,总有一款适合你
40 0
分享21个广告排行、15个交友会员、25个网站导航和39个文件管理PHP源码,总有一款适合你
|
4月前
|
小程序 PHP 数据安全/隐私保护
php图片加水印函数
这里分享下php给图片加水印的几个自定义函数 给图片加水印首先需要开启GD库。 用到的php函数是imagecopymerge () 和 imagecopy () imagecopymerge 函数可以支持两个图像叠加时,设置叠加的透明度
43 0
|
6月前
|
PHP
【PHP】读取本地文件夹中所有图片并显示
PHP图片收集系统收集作业后,为了方便老师在线查阅作业,特意写了个读取图片然后显示出来的php 比较粗糙,可以再多美化美化
58 0
|
7月前
|
存储 自然语言处理 搜索推荐
php 外贸代购系统网站
php 外贸代购系统网站
74 0
|
26天前
|
缓存 PHP 数据库
PHP程序性能优化指南
在当今互联网快速发展的时代,PHP作为一种流行的服务器端脚本语言,其性能优化显得尤为重要。本文将介绍一些提升PHP程序性能的有效方法,帮助开发者更好地优化他们的代码,提升应用程序的响应速度和效率。
|
1月前
|
存储 PHP Apache
使用CFimagehost源码搭建无需数据库支持的PHP免费图片托管私人图床
使用CFimagehost源码搭建无需数据库支持的PHP免费图片托管私人图床