当前位置:文章中心 - ASP编程 - 用ASP取出HTML里面的图片地址的函数
相关文章
这里放搜索
最新文章
用ASP取出HTML里面的图片地址的函数
关键词:ASP,HTML,图片地址,函数 时间:2008年09月15日 星期一 阅读:89

ASP取出HTML里面的图片地址函数主要原理就是用正则判断的属性。这在采集程序中将非常有用。

  1. Function ShowPic(str)    
  2. Set objRegExp = New Regexp'设置配置对象     
  3. objRegExp.IgnoreCase = True'忽略大小写     
  4. objRegExp.Global = True'设置为全文搜索     
  5. objRegExp.Pattern = "<img.+?>"    
  6. '为了确保能准确地取出图片地址所以分为两层配置:首先找到里面的img标签,然后再取出里面的图片地址后面的getimgs函数就是实现后一个功能的。     
  7. strs=trim(str)     
  8. Set Matches =objRegExp.Execute(strs)'开始执行配置     
  9. For Each Match in Matches     
  10. RetStr = RetStr &getimgs( Match.Value )'执行第二轮的匹配     
  11. Next     
  12. ShowPic = RetStr    
  13. End Function    
  14. Function getimgs(str)     
  15. getimgs=""     
  16. Set objRegExp1 = New Regexp     
  17. objRegExp1.IgnoreCase = True     
  18. objRegExp1.Global = True     
  19. objRegExp1.Pattern = "http://.+?"""'取出里面的地址     
  20. set mm=objRegExp1.Execute(str)     
  21. For Each Match1 in mm     
  22. getimgs=getimgs&left(Match1.Value,len(Match1.Value)-1)&"||"'把里面的地址串起来备用     
  23. next     
  24. End Function     
  25. '取得图片内容    
  26. function getHTTPPage(url)     
  27. on error resume next     
  28. dim http     
  29. set http=server.createobject("MSXML2.XMLHTTP")'使用xmlhttp的方法来获得图片的内容     
  30. Http.open "GET",url,false     
  31. Http.send()     
  32. if Http.readystate<>4 then     
  33. exit function     
  34. end if     
  35. getHTTPPage=Http.responseBody     
  36. set http=nothing     
  37. if err.number<>0 then err.Clear     
  38. end function     
  39. '保存图片    
  40. function saveimage(from,tofile)     
  41. dim geturl,objStream,imgs     
  42. geturl=trim(from)     
  43. imgs=gethttppage(geturl)'取得图片的具休内容的过程     
  44. Set objStream = Server.CreateObject("ADODB.Stream")'建立ADODB.Stream对象,必须要ADO 2.5以上版本     
  45. objStream.Type =1'以二进制模式打开     
  46. objStream.Open     
  47. objstream.write imgs'将字符串内容写入缓冲     
  48. objstream.SaveToFile server.mappath(tofile),2'-将缓冲的内容写入文件     
  49. objstream.Close()'关闭对象     
  50. set objstream=nothing     
  51. end function     
  52. '调用实例    
  53. Dim strpic,i,fname    
  54. strpic = ShowPic("  
  55. ")    
  56. strpic = Split(strpic,"||")    
  57. If UBound(strpic) > 0 Then     
  58. For i = 0 To UBound(strpic) - 1    
  59. '保存图片    
  60. fname=cstr(i&mid(strpic(i),instrrev(strpic(i),".")))     
  61. saveimage(strpic(i),fname)    
  62. Next    
  63. Else    
  64. End If 
上一篇:适合所有表的添加、删除、修改的函数 下一篇:asp处理xml数据的发送、接收类

0条记录访客评论

暂未有任何评论,你来发表一篇吧!

发表评论

(必填)
(必填)
 
友情链接