方法一.原理:读取目标网页的源代码,然后在写入到另一个静态文件中,可实现批量生成,也可实现一对一生成静态页面
<%
'常用函数
'1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码
function getHTTPPage(url)
dim Http
set Http=server.createobject("MSXML2.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
set http=nothing
if err.number<>0 then err.Clear
end function
'2、转换乱玛,直接用xmlhttp调用有中文字符的网页得到的将是乱玛,可以通过adodb.stream组件进行
转换
Function BytesToBstr(body,Cset)
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 = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
html="index.asp,HotSell.asp,contact.asp,plan.asp"
html1=split(html,",")
for i=0 to ubound(html1)
html2=split(html1(i),".")
txtURL="http://127.0.0.1/1/"&html1(i)'得到网页HTML代码
'Response.Write txtURL&"<br>"
sText = getHTTPPage(txtURL)
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
filename="../"&html2(0)&".htm";'生成页面
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立
openFile.writeline(sText)
Set OpenFile=nothing
next
%>
<script>
alert("静态网页生成完毕");
history.back();
</script>
方法二.就是利用模板来生成静态页面,提前写好模板内容,然后利用替换函数替换模板中的关键字,然后在利用FSO写入另一个HTML文件中.
<!--#include file="inc/conn.asp"-->
pencat是模板
<%
set rs=server.createobject("adodb.recordset")
sqltxt="select * from news" '读去新闻内容
rs.open sqltxt,conn,3,2
x=0
do while rs.eof=false
set rs1=server.createobject("adodb.recordset")
sql="select * from mb where id=1" '读去模版内容这样可以自由选择多个模版
rs1.open sql,conn,3,2
pencat=rs1("content")
title=rs("d_title") '标题
t=rs("d_addtime") '加入时间
ly=rs("d_author") '来源
content=rs("d_content") '文章内容
id=rs("d_id") '唯一识别的编号
zz=rs("d_editor") '作者
pencat=replace(pencat,"$TITLE$",title) '把标签为$TITLE$的替换为现在的标题
pencat=replace(pencat,"$TIME$",time) '把标签为$TIME$的替换为现在的时间
pencat=replace(pencat,"$FORM$",ly) '把标签为$FORM$的替换为现在的来源
pencat=replace(pencat,"$CONTENT$",content) '把标签为$CONTENT$的替换为现在的文章内容
pencat=replace(pencat,"$zz$",ly)
t=replace(t,"-","") '把时间中的- : 空格全部取掉
t=replace(t,":","")
t=replace(t," ","")
t=t&id '用时间和ID命名文件名
'Set fso1 = CreateObject("Scripting.FileSystemObject")
'fso1.DeleteFile(server.mappath (filePath& "/" & filen))
'Set fso1 = nothing
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(server.mappath("2006/" & t &".htm")) '建立一个后缀为 .htm文件
fout.WriteLine pencat '把替换后的模版pencat写入HTM文件中
fout.close
set fso = nothing
url="2006/" & t &".htm" '用时间命名HTM文件
rs("D_LinkUrl")=url
rs.update
response.write x+1&".<a href='"&url&"' target='_blank'>"
response.write title
response.write "</a> 已经完成<br><br>"
rs.movenext
x=x+1
loop
%>
模板内容
<TABLE width="100%" height="505" border=0 cellPadding=0 cellSpacing=0>
<TBODY>
<TR>
<TD align=middle bgColor=#cccccc height=1></TD>
</TR>
<TR>
<TD height=30 align="center"><span class="newstitle">$TITLE$</span></TD>
</TR>
<TR>
<TD height=37 align="center"><font color=#565656>发布时间:$TIME$ 来源:$FORM$ 作者:$zz$ </font></TD>
</TR>
<TR>
<TD height=37 align="left"><span class="news"> $CONTENT$ </span></TD>
</TR>
<TR vAlign=top>
<TD height=400> </TD>
</TR>
</TBODY>
</TABLE>
<!--#include file="inc/conn.asp"-->
<%
page=request("page") '第几页
id=request("id")
y=1000 '每页显示的字数
set rs=server.createobject("adodb.recordset")
sqltxt="select * from new where id="&id
rs.open sqltxt,conn,3,2
x=Len(rs("content")) ' 统计文章内容的字数
p=(x-(x mod y))/y '显示多少页
p=cint(p)
if page=0 or page="" then '判断页数为空怎显示第一页内容
page=0
end if
zs=page*y
if zs=0 then
zs=1
end if
content=mid(content,zs,y) '从文章中取字数
response.write content '输出字数
%>
<%
for i=0 to p step 1
%>
<a href='fy.asp?id=<%=id%>&page=<%=i%>'><%=i+1%></a>
<%next%> '输出页数