ASP生成静态网页收集整理

来源:互联网 发布:人工智能三定律 编辑:程序博客网 时间:2024/06/05 14:51
生成静态网页收集整理 ASP

1.使用FSO生成

<%
'使用FSO生成
Set fs = CreateObject("Scripting.FileSystemObject")
NewFile=Server.MapPath("ud03/fso.htm")
'新建一文件fso.htm,若该文件已存在,则覆盖它
Set a = fs.CreateTextFile(NewFile, True)
Response.Write"新文件已建立!"
a.close
File=Server.MapPath("ud03/fso.htm")
Set txt=fs.OpenTextFile(File,8,True) '打开成可以在结尾写入数据的文件
data1="这句话是使用WriteLine方法写入的。!<Br>"
txt.WriteLine data1
data2="这句话是使用Write方法写入的。<Br>"
txt.Write data2
txt.Close
%>

2.使用XMLHTTP生成

<%
'使用XMLHTTP生成
Set xml = Server.CreateObject("Microsoft.XMLHTTP")
'把下面的地址替换成你的首页的文件地址,一定要用http://开头的绝对路径,不能写相对路径
xml.Open "GET", "http://www.kinoko.name/ud03/", False
xml.Send
BodyText=xml.ResponseBody
BodyText=BytesToBstr(BodyText,"gb2312")
Set xml = Nothing
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile= fso.CreateTextFile(server.MapPath("ud03.htm"), True) '生成的html的文件名
MyFile.WriteLine(BodyText)
MyFile.Close

'使用Adodb.Stream处理二进制数据
Function BytesToBstr(strBody,CodeBase)
    dim objStream
    set objStream = Server.CreateObject("Adodb.Stream")
    objStream.Type = 1
    objStream.Mode =3
    objStream.Open
    objStream.Write strBody
    objStream.Position = 0
    objStream.Type = 2
    objStream.Charset = CodeBase
    BytesToBstr = objStream.ReadText
    objStream.Close
    set objStream = nothing
End Function
%>

3.使用XMLHTTP批量生成

<%
'使用XMLHTTP批量生成
dim strUrl,Item_Classid,id,FileName,FilePath,Do_Url,Html_Temp
Html_Temp="<UL>"
For i=1 To 30 '需要生成的id:1到30
Html_Temp = Html_Temp&"<LI>"
Item_Classid = i
FileName = "Archives_"&Item_Classid&".htm" '生成的html文件名
FilePath = Server.MapPath("/")&"/"&FileName
Html_Temp = Html_Temp&FilePath&"</LI>"
Do_Url = "http://www.kinoko.name/ud03/index.php" 'WEB路径
Do_Url = Do_Url&"?p="&Item_Classid 'WEB路径之后的ID
strUrl = Do_Url
dim objXmlHttp
set objXmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
objXmlHttp.open "GET",strUrl,false
objXmlHttp.send()
Dim binFileData
binFileData = objXmlHttp.responseBody
Dim objAdoStream
set objAdoStream = Server.CreateObject("ADODB.Stream")
objAdoStream.Type = 1
objAdoStream.Open()
objAdoStream.Write(binFileData)
objAdoStream.SaveToFile FilePath,2
objAdoStream.Close()
Next
Html_Temp = Html_Temp&"<UL>"
%>
<%
Response.Write ( "成功生成文件:" )
Response.Write ( "<BR>" )
Response.Write Html_Temp
%>

 

4.自动按模板生成网站首页


<%
Response.Expires = 0
Response.expiresabsolute = Now() - 1
Response.addHeader "pragma", "no-cache"
Response.addHeader "cache-control", "private"
Response.CacheControl = "no-cache"
Response.Buffer = True
Response.Clear
Server.ScriptTimeOut=999999999
on error resume next
'***************************************************************
'*                         定义 从模板从读取首页 函数
'* 说明:模板文件名为:index_Template.asp
'***************************************************************
Function GetPage(url)
         Set Retrieval = CreateObject("Microsoft.XMLHTTP")
         With Retrieval
         .Open "Get", url, False, "", ""
         .Send
         GetPage = BytesToBstr(.ResponseBody)
         End With
         Set Retrieval = Nothing
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"
         BytesToBstr = objstream.ReadText
         objstream.Close
         set objstream = nothing
End Function

'***************************************************************
'* 生页首页,文件名为:default.htm
'***************************************************************
dim Tstr
Tstr = GetPage("http://www.adhome.net/index_Template.asp")
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(Server.MapPath(".")&"/default.htm")
fout.Write Tstr
fout.close
    Response.write"<script>alert(""生成首页成功!/n/n文件名为:default.htm"");location.href="http://www.adhome.net";</script>"
    Response.end
%>

5.将asp页面转换成htm页面

<%
Function GetPage(url)
'获得文件内容
dim Retrieval
Set Retrieval = CreateObject("Microsoft.XMLHTTP")
With Retrieval
    .Open "Get", url, False ', "", ""
    .Send
    GetPage = BytesToBstr(.ResponseBody)
End With
Set Retrieval = Nothing
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"
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
on error resume next
Url="http://www.sina.com.cn"'要读取的页面地址
response.write "开始更新首页..."
wstr = GetPage(Url)
'response.write(wstr)
Set fs=Server.CreateObject("Scripting.FileSystemObject")
'if not MyFile.FolderExists(server.MapPath("/html/")) then
'MyFile.CreateFolder(server.MapPath("/html/"))'
'end if
'要存放的页面地址
dizhi=server.MapPath("index.htm")
If (fs.FileExists(dizhi)) Then
fs.DeleteFile(dizhi)
End If
Set CrFi=fs.CreateTextFile(dizhi)
Crfi.Writeline(wstr)
set CrFi=nothing
set fs=nothing
response.write "...<font color=red>更新完成!</font>"
%>

代码算是最简单的,直接保存成一个asp文件即可,只要把URL(要转化的asp地址)和dizhi(要保存的html地址)设置好就可以了,一般这两个文件在同一个目录,才能保证图片或者css、js起作用。


6.下面是利用XMLHTTP将动态网页生成静态网页的一段简单代码。

如一个正常的index.asp页面,并且用ASP代码调出数据库中的内容,另建一个makehtml.asp的页面,加入一个textarea域,假设为name="body",将index.asp在textarea里调出来,如:
<textarea name="body"><!--#include file="index.asp"--></textarea>

将这个textarea包含在表单中,在接收表单页用创建FSO对象,如下生成index.html文件!

<%
filename="../index.html"
if request("body")<>"" then
set fso = Server.CreateObject("Scripting.FileSystemObject")
set fout = fso.CreateTextFile(server.mappath(""&filename&""))
fout.write request.form("body")
fout.close
set fout=nothing
set fso=nothing
end if
%>

这样index.html文件就生成了,连模板都用不着,只要将正常情况下使用的ASP文件读取到textarea里就可以了,目前尚未发现问题!当然前提是服务器要支持FSO。

开启FSO权限 在 开始-“运行”中执行regsvr32.exe scrrun.dll即可。如想关闭FSO权限,在上述命令中加/u参数。注册表中的键值位置:HKEY_CLASS_BOOT/F.S.O .FSO中有个方法是CreateFolder,但是这个方法只能在其上一级文件夹存在的情况下创建新的文件夹,所以我就写了一个自动创建多级文件夹的函数,在生成静态页面等方面使用非常方便。函数:

<%
’ --------------------------------
’ 自动创建指定的多级文件夹
’ strPath为绝对路径
Function AutoCreateFolder(strPath) ’ As Boolean
         On Error Resume Next
         Dim astrPath, ulngPath, i, strTmpPath
         Dim objFSO
         If InStr(strPath, "/") <=0 Or InStr(strPath, ":") <= 0 Then
                 AutoCreateFolder = False
                 Exit Function
         End If
         Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
         If objFSO.FolderExists(strPath) Then
                 AutoCreateFolder = True
                 Exit Function
         End If
         astrPath = Split(strPath, "/")
         ulngPath = UBound(astrPath)
         strTmpPath = ""
         For i = 0 To ulngPath
                 strTmpPath = strTmpPath & astrPath(i) & "/"
                 If Not objFSO.FolderExists(strTmpPath) Then
                         ’ 创建
                         objFSO.CreateFolder(strTmpPath)
                 End If
         Next
         Set objFSO = Nothing
         If Err = 0 Then
                 AutoCreateFolder = True
         Else
                 AutoCreateFolder = False
         End If
End Function 调用方法:

MyPath = "C:/a/b/c/"
If AutoCreateFolder(MyPath) Then
         Response.Write "创建文件夹成功"
Else
         Response.Write "创建文件夹失败"
End If
%>

原创粉丝点击