-
asp.net教程之用ASP取出HTML里面的图片地址的函数
用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 = "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=""图片地址"" 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
函数如下:
以下是引用片段:
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=""图片地址"" 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
栏目列表
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比