一,取得原頁中的圖片的地址。 function PicStr(str)
?Set objRegExp = New Regexp '設(shè)置配置對象
?objRegExp.IgnoreCase = True '忽略大小寫
?objRegExp.Global = True '設(shè)置為全文搜索
?objRegExp.Pattern = "
" '為了確保能準(zhǔn)確地取出圖片地址所以分為兩層配置:首先找到里面的
標(biāo)簽,然后再取出里面的圖片地址后面的getimgs函數(shù)就是實(shí)現(xiàn)后一個功能的。
?strs=trim(str)
?Set Matches =objRegExp.Execute(strs) '開始執(zhí)行配置
?For Each Match in Matches
?PicStr = PicStr &getimgs( Match.Value ) '執(zhí)行第二輪的匹配
?Next
?'所有的圖片在里面都是這樣的src="http://圖片的地址",所以可以這樣來取得確切的圖片地址
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
%>
二,下載圖片并保存在服務(wù)器上。 function getHTTPPage(url)
??on error resume next
??dim http
??set http=server.createobject("MSXML2.XMLHTTP") '使用xmlhttp的方法來獲得圖片的內(nèi)容
??Http.open "GET",url,false
??Http.send()
??if Http.readystate4 then
??exit function
??end if
??getHTTPPage=Http.responseBody
??set http=nothing
??if err.number0 then err.Clear
end function
'取得了圖片的內(nèi)容要保存,給人一種感覺是用FSO來作就可以了,但實(shí)際上不行,這樣保存程序就會出錯,因?yàn)镕SO不支持流式的文件,所以我們要調(diào)用另一個對象:ADO.STREM。具體的過程如下:
function saveimage(from,tofile)
??dim geturl,objStream,imgs
??geturl=trim(from)
??imgs=gethttppage(geturl)'取得圖片的具休內(nèi)容的過程
??Set objStream = Server.CreateObject("ADODB.Stream")'建立ADODB.Stream對象,必須要ADO 2.5以上版本
??objStream.Type =1'以二進(jìn)制模式打開
??objStream.Open
??objstream.write imgs'將字符串內(nèi)容寫入緩沖
??objstream.SaveToFile server.mappath(tofile),2'-將緩沖的內(nèi)容寫入文件
??objstream.Close()'關(guān)閉對象
??set objstream=nothing
end function
'所以只要用一個循環(huán)來把剛才取得的地址中的圖片全部保存下來,具體過程如下:
arrimg=split(PicStr(str),"||") '分割字串,取得里面地址列表
allimg=""
newimg=""
for i=1 to ubound(arrimg)
if arrimg(i)"" and instr(allimg,arrimg(i))fname=baseurl&cstr(i&mid(arrimg(i),instrrev(arrimg(i),".")))
saveimage(arrimg(i),fname)‘保存地址的函數(shù),過程見上面
allimg=allimg&"||"&arrimg(i) '把保存下來的圖片的地址串回起來,以確定要替換的地址
newimg=newimg&"||"&fname '把本地的地址串回起來
end if
next
'第三步就是替換原來的地址了。具體的過程就是下面了:
arrnew=split(newimg,"||") '取得原來的圖片地址列表
arrall=split(allimg,"||") '取得已經(jīng)保存下來的圖片的地址列表
for i=1 to ubound(arrnew) '執(zhí)行循環(huán)替換原來的地址
??strs=replace(strs,arrall(i),arrnew(i))
next
%>