以下是取出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://img.knowsky.com/img/knowskylogo.gif"" 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
|