以下是取出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://www.wind88.net/picture/20070206/20072615561663.jpg"" 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

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 11 的頭像
    11

    冠霖的部落格

    11 發表在 痞客邦 留言(0) 人氣()