取出HTML里面的图片地址
2007-06-12 14:54
351 查看
以下是取出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
主要原理就是用正则判断 <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
相关文章推荐
- 用ASP取出HTML里面的图片地址的函数
- 用ASP取出HTML里面的图片地址的函数
- 用ASP取出HTML里面的图片地址的函数
- 用ASP取出HTML里面的图片地址的函数
- 用ASP取出HTML里面的图片地址的函数
- Python---对html文件内容进行搜索取出特定URL地址字符串,保存成列表,并使用每个url下载图片,并保存到硬盘上,使用正则re
- Python---对html文件内容进行搜索取出特定URL地址字符串,保存成列表,并使用每个url下载图片,并保存到硬盘上,使用bs4,beautifulsoup模块
- ASP.NET去除HTML标记,取出文本中的图片地址,移除HTML标签
- 获取HTML里面的超链接,图片,FLASH地址
- 取出内容中插入图片的地址
- php正则提取html图片(img)src地址与任意属性的方法
- php正则提取html图片(img)src地址与任意属性
- 表达式从网页html代码中提取图片的scr地址!
- Android中将图片从相册取出且获取到图片在手机中的地址
- ios 提取html 字符串中的img 的地址(图片地址)
- [导入][原创]使用正则表达式找出HTML代码内所有IMG图片的SRC链接地址
- js获取html页面代码中图片地址的实现代码
- Java 使用正则表达式取出图片地址以及跳转的链接地址,来判断死链(一)
- php-curl获取接口图片地址,在php拼接img中无法访问,报403错误,在浏览器url中直接可以访问,html页面里img也可以访问
- php正则提取html图片(img)src地址与任意属性