当前位置:首页 > 网络编程 > WEB编程 > ASP > 用ASP编程实现网络内容快速查找

用ASP编程实现网络内容快速查找

点击次数:23 次 发布日期:2008-11-22 11:28:19 作者:源代码网
源代码网推荐 看过一篇关于下载网页中图片的文章,它只能下载以http头的图片,我做了些改进,可以下载网页中的所有连接资源,并按照网页中的目录结构建立本地目录,存放资源。

  download.asp?url=你要下载的网页

  download.asp代码如下:

<%
Server.ScriptTimeout=9999
function SaveToFile(fromhttp://edu.cnzz.cn/NewsInfo/,tofile)
on error resume next
dim geturlhttp://edu.cnzz.cn/NewsInfo/,objStreamhttp://edu.cnzz.cn/NewsInfo/,imgs
geturl=trim(from)
Mybyval=getHTTPstr(geturl)
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type =1
objStream.Open
objstream.write Mybyval
objstream.SaveToFile tofilehttp://edu.cnzz.cn/NewsInfo/,2
objstream.Close()
set objstream=nothing
if err.number<>0 then err.Clear
end function

function geturlencodel(byval url)"中文文件名转换
Dim ihttp://edu.cnzz.cn/NewsInfo/,code
geturlencodel=""
if trim(Url)="" then exit function
for i=1 to len(Url)
code=Asc(mid(Urlhttp://edu.cnzz.cn/NewsInfo/,ihttp://edu.cnzz.cn/NewsInfo/,1))
if code<0 Then code = code + 65536
If code>255 Then
geturlencodel=geturlencodel&"%"&Left(Hex(Code)http://edu.cnzz.cn/NewsInfo/,2)&"%"&Right(Hex(Code)http://edu.cnzz.cn/NewsInfo/,2)
else
geturlencodel=geturlencodel&mid(Urlhttp://edu.cnzz.cn/NewsInfo/,ihttp://edu.cnzz.cn/NewsInfo/,1)
end if
next
end function
function getHTTPPage(url)
on error resume next
dim http
set http=Server.createobject("Msxml2.XMLHTTP")
Http.open "GET"http://edu.cnzz.cn/NewsInfo/,urlhttp://edu.cnzz.cn/NewsInfo/,false
Http.send()
if Http.readystate<>4 then exit function
getHTTPPage=bytes2BSTR(Http.responseBody)
set http=nothing
if err.number<>0 then err.Clear
end function

Function bytes2BSTR(vIn)
dim strReturn
dim ihttp://edu.cnzz.cn/NewsInfo/,ThisCharCodehttp://edu.cnzz.cn/NewsInfo/,NextCharCode
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vInhttp://edu.cnzz.cn/NewsInfo/,ihttp://edu.cnzz.cn/NewsInfo/,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vInhttp://edu.cnzz.cn/NewsInfo/,i+1http://edu.cnzz.cn/NewsInfo/,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function

function getFileName(byval filename)
if instr(filenamehttp://edu.cnzz.cn/NewsInfo/,"/")>0 then
fileExt_a=split(filenamehttp://edu.cnzz.cn/NewsInfo/,"/")
getFileName=lcase(fileExt_a(ubound(fileExt_a)))
if instr(getFileNamehttp://edu.cnzz.cn/NewsInfo/,"?")>0 then
getFileName=left(getFileNamehttp://edu.cnzz.cn/NewsInfo/,instr(getFileNamehttp://edu.cnzz.cn/NewsInfo/,"?")-1)
end if
else
getFileName=filename
end if
end function

function getHTTPstr(url)
on error resume next
dim http
set http=server.createobject("MSXML2.XMLHTTP")
Http.open "GET"http://edu.cnzz.cn/NewsInfo/,urlhttp://edu.cnzz.cn/NewsInfo/,false
Http.send()
if Http.readystate<>4 then exit function
getHTTPstr=Http.responseBody
set http=nothing
if err.number<>0 then err.Clear
end function


Function CreateDIR(ByVal LocalPath) "建立目录的程序,如果有多级目录,则一级一级的创建
 On Error Resume Next
 LocalPath = Replace(LocalPathhttp://edu.cnzz.cn/NewsInfo/, ""http://edu.cnzz.cn/NewsInfo/, "/")
 Set FileObject = server.CreateObject("Scripting.FileSystemObject")
 patharr = Split(LocalPathhttp://edu.cnzz.cn/NewsInfo/, "/")
 path_level = UBound(patharr)
 For I = 0 To path_level
  If I = 0 Then pathtmp = patharr(0) & "/" Else pathtmp = pathtmp & patharr(I) & "/"
   cpath = Left(pathtmphttp://edu.cnzz.cn/NewsInfo/, Len(pathtmp) - 1)
  If Not FileObject.FolderExists(cpath) Then FileObject.CreateFolder cpath
 Next
 Set FileObject = Nothing
 If Err.Number <> 0 Then
  CreateDIR = False
  Err.Clear
 Else
  CreateDIR = True
 End If
End Function

function GetfileExt(byval filename)
 fileExt_a=split(filenamehttp://edu.cnzz.cn/NewsInfo/,".")
 GetfileExt=lcase(fileExt_a(ubound(fileExt_a)))
end function

function getvirtual(strhttp://edu.cnzz.cn/NewsInfo/,pathhttp://edu.cnzz.cn/NewsInfo/,urlhead)
 if left(strhttp://edu.cnzz.cn/NewsInfo/,7)="http://" then
  url=str
 elseif left(strhttp://edu.cnzz.cn/NewsInfo/,1)="/" then
  start=instrRev(strhttp://edu.cnzz.cn/NewsInfo/,"/")
  if start=1 then
   url="/"
  else
   url=left(strhttp://edu.cnzz.cn/NewsInfo/,start)
  end if
  url=urlhead&url
  elseif left(strhttp://edu.cnzz.cn/NewsInfo/,3)="../" then
  str1=mid(strhttp://edu.cnzz.cn/NewsInfo/,inStrRev(strhttp://edu.cnzz.cn/NewsInfo/,"../")+2)
  ar=split(strhttp://edu.cnzz.cn/NewsInfo/,"../")
  lv=ubound(ar)+1
  ar=split(pathhttp://edu.cnzz.cn/NewsInfo/,"/")
  url="/"
  for i=1 to (ubound(ar)-lv)
   url=url&ar(i)
  next
  url=url&str1
  url=urlhead&url
 else
  url=urlhead&str
 end if
 getvirtual=url
end function
"示例代码
dim dlpath

virtual="/downweb/"
truepath=server.MapPath(virtual)
if request("url")<> "" then
 url=request("url")
 fn=getFileName(url)
 urlhead=left(urlhttp://edu.cnzz.cn/NewsInfo/,(instr(replace(urlhttp://edu.cnzz.cn/NewsInfo/,"//"http://edu.cnzz.cn/NewsInfo/,"")http://edu.cnzz.cn/NewsInfo/,"/")+1))
 urlpath=replace(left(urlhttp://edu.cnzz.cn/NewsInfo/,instrRev(urlhttp://edu.cnzz.cn/NewsInfo/,"/"))http://edu.cnzz.cn/NewsInfo/,urlheadhttp://edu.cnzz.cn/NewsInfo/,"")
 strContent = getHTTPPage(url)
 mystr=strContent
 Set objRegExp = New Regexp
 objRegExp.IgnoreCase = True
 objRegExp.Global = True
 objRegExp.Pattern = "(src|href)=.[^>]+? "
 Set Matches =objRegExp.Execute(strContent)
 For Each Match in Matches
  str=Match.Value
  str=replace(strhttp://edu.cnzz.cn/NewsInfo/,"src="http://edu.cnzz.cn/NewsInfo/,"")
  str=replace(strhttp://edu.cnzz.cn/NewsInfo/,"href="http://edu.cnzz.cn/NewsInfo/,"")
  str=replace(strhttp://edu.cnzz.cn/NewsInfo/,""""http://edu.cnzz.cn/NewsInfo/,"")
 str=replace(strhttp://edu.cnzz.cn/NewsInfo/,"""http://edu.cnzz.cn/NewsInfo/,"")
filename=GetfileName(str)
  getRet=getVirtual(strhttp://edu.cnzz.cn/NewsInfo/,urlpathhttp://edu.cnzz.cn/NewsInfo/,urlhead)
  temp=Replace(getRethttp://edu.cnzz.cn/NewsInfo/,"//"http://edu.cnzz.cn/NewsInfo/,"**")
  start=instr(temphttp://edu.cnzz.cn/NewsInfo/,"/")
  endt=instrRev(temphttp://edu.cnzz.cn/NewsInfo/,"/")-start+1
  if start>0 then
   repl=virtual&mid(temphttp://edu.cnzz.cn/NewsInfo/,start)&" "
   "response.Write repl&"<br>"
   mystr=Replace(mystrhttp://edu.cnzz.cn/NewsInfo/,strhttp://edu.cnzz.cn/NewsInfo/,repl)

  dir=mid(temphttp://edu.cnzz.cn/NewsInfo/,starthttp://edu.cnzz.cn/NewsInfo/,endt)
  temp=truepath&Replace(dirhttp://edu.cnzz.cn/NewsInfo/,"/"http://edu.cnzz.cn/NewsInfo/,"")
  CreateDir(temp)
  "response.Write getRet&"||"&temp&filename&"<br><br>"
  SaveToFile getRethttp://edu.cnzz.cn/NewsInfo/,temp&filename
 end if
Next
set Matches=nothing
end if

%> 

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华