喜欢本站并加入收藏
首 页 电脑学园 技术文档 图形图像 办公应用 网页设计 管理资料 PPT模板 方案合同
 位置: 北方教程网 >> 网页设计 >> Asp专题 >> 正文
   ASP取HTML里面图片地址的函数
ASP取HTML里面图片地址的函数
[ 作者:佚名   来源:网络整理   点击数:   更新时间:2007-3-4 ]

下面是取出HTML里面的图片地址的函数:

主要原理就是用正则判断 <img> 的<src>属性。这在采集程序中将非常有用。

函数如下:

以下是引用片段:
Function ShowPic(str)
 Set objRegExp = New Regexp'设置配置对象 
 objRegExp.IgnoreCase = True'忽略大小写 
 objRegExp.Global = True'设置为全文搜索 
 objRegExp.Pattern = "<img.+?>"
 '为了确保能准确地取出图片地址所以分为两层配置:首先找到里面的<img>标签,然后再取出里面的图片地址后面的getimgs函数就是实现后一个功能的。 
 strs=trim(str) 
 Set Matches =objRegExp.Execute(strs)'开始执行配置 
 For Each Match in Matches 
  RetStr = RetStr &getimgs( Match.Value )'执行第二轮的匹配 


 Next 
 ShowPic = RetStr
End Function
Function getimgs(str) 
 getimgs="" 
 Set objRegExp1 = New Regexp 
 objRegExp1.IgnoreCase = True 
 objRegExp1.Global = True 
 objRegExp1.Pattern = "http://.+?"""'取出里面的地址 
 set mm=objRegExp1.Execute(str) 
 For Each Match1 in mm 
  getimgs=getimgs&left(Match1.Value,len(Match1.Value)-1)&"||"'把里面的地址串起来备用 
 next 
End Function 
'取得图片内容
function getHTTPPage(url) 
 on error resume next 
 dim http 
 set http=server.createobject("MSXML2.XMLHTTP")'使用xmlhttp的方法来获得图片的内容 
 Http.open "GET",url,false 
 Http.send() 
 if Http.readystate<>4 then 
 exit function 
 end if 
 getHTTPPage=Http.responseBody 
 set http=nothing 
 if err.number<>0 then err.Clear 
end function 
'保存图片
function saveimage(from,tofile) 
 dim geturl,objStream,imgs 
 geturl=trim(from) 
 imgs=gethttppage(geturl)'取得图片的具休内容的过程 
 Set objStream = Server.CreateObject("ADODB.Stream")'建立ADODB.Stream对象,必须要ADO 2.5以上版本 
 objStream.Type =1'以二进制模式打开 
 objStream.Open 
 objstream.write imgs'将字符串内容写入缓冲 
 objstream.SaveToFile server.mappath(tofile),2'-将缓冲的内容写入文件 
 objstream.Close()'关闭对象 
 set objstream=nothing 
end function 

'调用实例
Dim strpic,i,fname
strpic = ShowPic("<DIV align=center><IMG src=""HTTP://www.wind88.net/picture/20070206/20072615561663.jpg"" border=0></DIV>")


strpic = Split(strpic,"||")
If UBound(strpic) > 0 Then 
 For i = 0 To UBound(strpic) - 1
  '保存图片
  fname=cstr(i&mid(strpic(i),instrrev(strpic(i),"."))) 
  saveimage(strpic(i),fname)
 Next
Else
End If

上一篇文章: asp全站防止注入的代码 下一篇文章: ASP使用MySQL数据库的方法
打印此文  收藏此页  关闭窗口  返回顶部
 相关文章  热点文章
 · 利用CSS改进网站设计的小技巧
 · Dreamweaver快捷操作技巧集锦
 · 使用Dreamweaver架设网站简明教程
 · 网页设计制作过程中常用的几个代码技巧
 · 高效使用CSS编码的方法
 
热点图文
教你节省Vist...
使用Windows ...
 · 远程管理Web服务器的方法技巧
 · Win Vista系统自带IIS7.0设置详解...
 · 详解Windows系统IIS6.0功能及应用...
 · 服务器集群系统实现方法及案例分...
 · IIS服务器实现asp,cgi和php+mysq...
 · IIS下SQL Server数据库安全设置技...
推 荐 阅 读
巧用Photosho...
Photoshop鼠绘...
 · Fireworks制作旋转的漂亮gif动画...
 · Photoshop制作都市满天繁星
 · Fireworks制作Menu按钮
 · 巧用Photoshop制作喜迎奥运壁纸
 · 巧用Photoshop钢笔工具绘制彩色美...
 · Photoshop制作浪漫动感花朵
 设为首页 - 加入收藏 - 关于本站 - 联系我们 - 广告合作 - 友情链接 - 在线交流 - 
CopyRight© 2006-2009 Eduai.Com All Rights Reserved
在线交流 QQ:524152258 辽ICP备07001851号
免责声明:本站部分资源来自网络,如有侵犯您的版权请通知,我们立即删除。