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
所有的图片在里面都是这样的src="http://图片的地址",所以可以这样来取得确切的图片地址:
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
所以只要用一个循环来把刚才取得的地址中的图片全部保存下来,具体过程如下:
arrimg=split(retstr,"||")'分割字串,取得里面地址列表
allimg=""
newimg=""
for i=1 to ubound(arrimg)
if arrimg(i)<>"" and instr(allimg,arrimg(i))<1 then'看这个图片是否已经下载过
fname=baseurl&cstr(i&mid(arrimg(i),instrrev(arrimg(i),".")))
saveimage(arrimg(i),fname)‘保存地址的函数,过程见上面
allimg=allimg&"||"&arrimg(i)'把保存下来的图片的地址串回起来,以确定要替换的地址
newimg=newimg&"||"&fname'把本地的地址串回起来
end if
next
第三步就是替换原来的地址了。
具体的过程就是下面了:
arrnew=split(newimg,"||")'取得原来的图片地址列表
arrall=split(allimg,"||")'取得已经保存下来的图片的地址列表
for i=1 to ubound(arrnew)'执行循环替换原来的地址
strs=replace(strs,arrall(i),arrnew(i))
next
cctv=strs