当前位置: 移动技术网 > IT编程>开发语言>.net > ASP正则判断取出HTML的图片地址的函数

ASP正则判断取出HTML的图片地址的函数

2019年01月19日  | 移动技术网IT编程  | 我要评论

qq宝贝论坛,伤感说唱,qq空间小窝素材

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

  函数如下:

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 = "https://.+?"""取出里面的地址  
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=""图片地址"" 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 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网