ASP取出HTML裡面的圖片地址的函數

來源:互聯網
上載者:User
以下是取出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



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.