用ASP取出HTML里面的图片地址的函数

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值