当前位置: 首页 > 图文教程 > 网络编程 > ASP > ASP正则判断取出HTML的图片地址的函数

ASP
asp生成三维饼图的函数
ASP下的简洁的多重查询的方法与函数 真不错
发一个ASP的ADODB类代码
A利用ASP小偷和Google实现在线翻译功能的代码
ASP类型网站结合动网论坛会员的方法
Asp下实现多表单域无组件文件上传的实例
asp制作中常用到的函数库集合
易心asp分页类 v1.0
asp中xmlhttp组件发包
检测是否为数字类型的asp代码
可以获得文件的文件名的asp函数
用asp实现访问远程计算机上MDB access数据库文件的方法
用ASP实现写IIS日志的代码
asp在IE浏览器中下载服务端上的各类文件的实现方法
asp汉字中文图片验证码的实现代码
asp WAP获取手机终端信息的一段代码
asp取得数组中的最大值的方法
用asp实现的截取指定格式字符串的代码
asp jmail发邮件 详细解析
ASP编程中连接数据库和数据库操作的常用代码

ASP正则判断取出HTML的图片地址的函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 63 ::
收藏到网摘: n/a

    用ASP取出HTML里面的图片地址的函数主要原理就是用正则判断的属性。这在采集程序中将非常有用。

  函数如下:

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=""图片地址"" 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