一聚教程网:一个值得你收藏的教程网站

热门教程

生成静态页面

时间:2022-07-02 23:17:51 编辑:袖梨 来源:一聚教程网

这是我初学asp时写的一个asp生成静态页面的方法,

<%dim fs
'---------------------------------------------------------------------------------------------
Function getHTTPPage(url)
    dim objXML
    set objXML=server.createobject("MSXML2.XMLHTTP")'定义
    objXML.open "GET",url,false'打开
    objXML.send()'发送
    If objXML.readystate<>4 then '判断文档是否已经解析完,以做客户端接受返回消息
        exit function
    End If
    getHTTPPage=BytesToBstr(objXML.responseBody)'返回信息,同时用函数定义编码
    set objXML=nothing'关闭
    if err.number<>0 then err.Clear
End Function
'----------------------------------------------------------------------------------------------
Function BytesToBstr(body)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
    objstream.Type = 1
    objstream.Mode =3
    objstream.Open
    objstream.Write body
    objstream.Position = 0
    objstream.Type = 2
    objstream.Charset = "GB2312"
    '转换原来默认的UTF-8编码转换成GB2312编码,否则直接用XMLHTTP调用有中文字符的网页得到的将是乱码
    BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
'----------------------------------------------------------------------------------------------------
function savafile(html,htmlfilename)
 chtml=html
 htmlfilename=server.MapPath(htmlfilename)
 call fso()
 set writerfile=fs.createtextfile(htmlfilename,true)
  writerfile.writeline chtml
  writerfile.close
           chtml=""
end function
'-----------------------------------------------------------------------------------------------------
function fso()
set fs=server.CreateObject("scripting.filesystemobject")
end function
'------------------------------------------------------------------------------------------------------
function htmlname(url)
 filename=mid(url,instrrev(url,"/")+1)
 newlen=len(filename)-len(mid(filename,instrrev(filename,".")+1))
    htmlname=mid(filename,1,newlen)&"html"
end function
'-----------------------------------------------------------------------------------------------------
function replaceall(html)
dim temp
temp=replace(html,"index.asp","index.html")
temp=replace(temp,"music.asp","music.html")
temp=replace(temp,"stuzp.asp","stuzp.html")
temp=replace(temp,"class.asp","class.html")
temp=replace(temp,"tch.asp","tch.html")
temp=replace(temp,"photo.asp","photo.html")
replaceall=replace(temp,"guestbook.asp","guestbook.html")
end function
'---------------------------------------------------------------------
Function getphoto(strHTML,key)
Dim objRegExp, Match, Matches
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = key
'
tp=""
Set Matches = objRegExp.Execute(strHTML)
For Each Match in Matches
tp=tp & Match.value
exit for
Next
getphoto=tp
Set objRegExp = Nothing
End Function
'---------------------------------------------------------
function pagehtml(html)
bb=getphoto(html,"")
if bb<>"" then
bb=mid(bb,10)
clen=len(bb)-3
bb=mid(bb,1,clen)
pagehtml=bb
end if
end function

'-------------------------------------------------------------------------
function syname(url)
 filename=mid(url,instrrev(url,"/")+1)
 newlen=len(filename)-len(mid(filename,instrrev(filename,".")))
    syname=mid(filename,1,newlen)
end function
dim arr(6)
arr(0)="http://www.111com.net/stu/index.asp"
arr(1)="http://www.111com.net/stu/music.asp"
arr(2)="http://www.111com.net/stu/stuzp.asp"
arr(3)="http://www.111com.net/stu/class.asp"
arr(4)="http://www.111com.net/stu/tch.asp"
arr(5)="http://www.111com.net/stu/photo.asp"
arr(6)="http://www.111com.net/stu/guestbook.asp"
for i=0 to 6
Url = arr(i)
url=arr(i)
topHtml = getHTTPPage(Url)
tophtml=replaceall(html)
topnames=htmlname(url)
s=pagehtml(html)
if s>1 then
 for j=1 to s
  fyhtml=gethttppage(url&"?page="&j)  
  names=syname(url)
  an=htmlname(url)
  fyhtml=replaceall(fyhtml) 
  bs0=names&j&".html"
 for q=1 to s
  htm=an&"?page="&q
  bs=names&q&".html"
  fyhtml=replace(fyhtml,htm,bs) 
 next  
  call savafile(fyhtml,bs0)
 next
end if
call savafile(tophtml,topnames)
chtml=""


if error =0 then
 response.Write("成功生成"&topnames)
else
 response.Write("生成"&topnames&"失败")
end if
response.flush
response.write("
")

next
%>

本站原创:转载请注明www.111com.net

热门栏目