查看: 5807|回复: 0
打印 上一主题 下一主题

用ASP取出HTML里面的图片地址的函数

[复制链接]
跳转到指定楼层
1#
发表于 2007-11-5 21:11:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
用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  


本篇文章来源于 站长资讯网 原文链接:http://webdevelop.chinahtml.com/1/2007/asp-119423097116910.shtml
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表