赵威SEO

关注SEO,SEM,网络营销,网站运营,和网络推广,喜欢小沈阳和北京国安

« 站内导航地图对搜索引擎收录数量的影响莫装逼,装逼早晚遭雷劈 »

ASP生成静态网页的方法搜集整理

因为我们知道搜索引擎比较喜欢静态页面,网页静态化也可以提高页面的打开速度,提高用户体验,减轻服务器压力等等。

所以我们在网站制作,网站优化的时候更多的是生成静态的信息页面和站点地图导航。

以下搜集整理了几种ASP生成静态的方法,供大家举一反三。

提醒,生成网页的时候可以适当在URL保护关键词。

1.使用FSO生成

ASP/Visual Basic代码
  1. <%    
  2. '使用FSO生成    
  3. Set fs = CreateObject("Scripting.FileSystemObject")    
  4. NewFile=Server.MapPath("ud03/fso.htm")    
  5. '新建一文件fso.htm,若该文件已存在,则覆盖它    
  6. Set a = fs.CreateTextFile(NewFile, True)    
  7. Response.Write"新文件已建立!"    
  8. a.close    
  9. File=Server.MapPath("ud03/fso.htm")    
  10. Set txt=fs.OpenTextFile(File,8,True'打开成可以在结尾写入数据的文件    
  11. data1="这句话是使用WriteLine方法写入的。!<Br>"    
  12. txt.WriteLine data1    
  13. data2="这句话是使用Write方法写入的。<Br>"    
  14. txt.Write data2    
  15. txt.Close    
  16. %>  

2.使用XMLHTTP生成

ASP/Visual Basic代码
  1. <%    
  2. '使用XMLHTTP生成    
  3. Set xml = Server.CreateObject("Microsoft.XMLHTTP")    
  4. '把下面的地址替换成你的首页的文件地址,一定要用http://开头的绝对路径,不能写相对路径    
  5. xml.Open "GET""http://www.kinoko.name/ud03/"False    
  6. xml.Send    
  7. BodyText=xml.ResponseBody    
  8. BodyText=BytesToBstr(BodyText,"gb2312")    
  9. Set xml = Nothing    
  10. Dim fso, MyFile    
  11. Set fso = CreateObject("Scripting.FileSystemObject")    
  12. Set MyFile= fso.CreateTextFile(server.MapPath("ud03.htm"), True'生成的html的文件名    
  13. MyFile.WriteLine(BodyText)    
  14. MyFile.Close   
  15.   
  16. '使用Adodb.Stream处理二进制数据    
  17. Function BytesToBstr(strBody,CodeBase)    
  18.     dim objStream    
  19.     set objStream = Server.CreateObject("Adodb.Stream")    
  20.     objStream.Type = 1    
  21.     objStream.Mode =3    
  22.     objStream.Open    
  23.     objStream.Write strBody    
  24.     objStream.Position = 0    
  25.     objStream.Type = 2    
  26.     objStream.Charset = CodeBase    
  27.     BytesToBstr = objStream.ReadText    
  28.     objStream.Close    
  29.     set objStream = nothing    
  30. End Function    
  31. %>   

3.使用XMLHTTP批量生成

ASP/Visual Basic代码
  1. <%    
  2. '使用XMLHTTP批量生成    
  3. dim strUrl,Item_Classid,id,FileName,FilePath,Do_Url,Html_Temp    
  4. Html_Temp="<UL>"    
  5. For i=1 To 30 '需要生成的id:1到30    
  6. Html_Temp = Html_Temp"<LI>"    
  7. Item_Classid = i    
  8. FileName = "Archives_"&Item_Classid".htm" '生成的html文件名    
  9. FilePath = Server.MapPath("/")"\"&FileName   
  10. Html_Temp = Html_Temp&FilePath&"</LI> 
  11. Do_Url = "http://www.kinoko.name/ud03/index.php" 'WEB路径   
  12. Do_Url = Do_Url&"?p="&Item_Classid 'WEB路径之后的ID   
  13. strUrl = Do_Url   
  14. dim objXmlHttp   
  15. set objXmlHttp = Server.CreateObject("Microsoft.XMLHTTP")   
  16. objXmlHttp.open "GET",strUrl,false   
  17. objXmlHttp.send()   
  18. Dim binFileData   
  19. binFileData = objXmlHttp.responseBody   
  20. Dim objAdoStream   
  21. set objAdoStream = Server.CreateObject("ADODB.Stream")   
  22. objAdoStream.Type = 1   
  23. objAdoStream.Open()   
  24. objAdoStream.Write(binFileData)   
  25. objAdoStream.SaveToFile FilePath,2   
  26. objAdoStream.Close()   
  27. Next   
  28. Html_Temp = Html_Temp&"<UL> 
  29. %>   
  30. <%   
  31. Response.Write ( "成功生成文件:" )   
  32. Response.Write ( "<BR>" )    
  33. Response.Write Html_Temp    
  34. %>   
  35.   

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

ASP/Visual Basic代码
  1. <%    
  2. Response.Expires = 0    
  3. Response.expiresabsolute = Now() - 1    
  4. Response.addHeader "pragma""no-cache"    
  5. Response.addHeader "cache-control""private"    
  6. Response.CacheControl = "no-cache"    
  7. Response.Buffer = True    
  8. Response.Clear    
  9. Server.ScriptTimeOut=999999999    
  10. on error resume next    
  11. '***************************************************************    
  12. '*                         定义 从模板从读取首页 函数    
  13. '* 说明:模板文件名为:index_Template.asp    
  14. '***************************************************************    
  15. Function GetPage(url)    
  16.          Set Retrieval = CreateObject("Microsoft.XMLHTTP")    
  17.          With Retrieval    
  18.          .Open "Get", url, False""""    
  19.          .Send    
  20.          GetPage = BytesToBstr(.ResponseBody)    
  21.          End With    
  22.          Set Retrieval = Nothing    
  23. End Function    
  24. Function BytesToBstr(body)    
  25.          dim objstream    
  26.          set objstream = Server.CreateObject("adodb.stream")    
  27.          objstream.Type = 1    
  28.          objstream.Mode =3    
  29.          objstream.Open    
  30.          objstream.Write body    
  31.          objstream.Position = 0    
  32.          objstream.Type = 2    
  33.          objstream.Charset = "GB2312"    
  34.          BytesToBstr = objstream.ReadText    
  35.          objstream.Close    
  36.          set objstream = nothing    
  37. End Function  
  38.   
  39. '***************************************************************    
  40. '* 生页首页,文件名为:default.htm    
  41. '***************************************************************    
  42. dim Tstr    
  43. Tstr = GetPage("http://www.adhome.net/index_Template.asp")    
  44. Set fso = Server.CreateObject("Scripting.FileSystemObject")    
  45. Set fout = fso.CreateTextFile(Server.MapPath(".")"/default.htm")    
  46. fout.Write Tstr    
  47. fout.close    
  48.     Response.write"<script>alert(""生成首页成功!\n\n文件名为:default.htm"");location.href="http://www.adhome.net";</script>"    
  49.     Response.end    
  50. %>   
  51.   
  52. 5.将asp页面转换成htm页面   
  53.   
  54. <%    
  55. Function GetPage(url)    
  56. '获得文件内容    
  57. dim Retrieval    
  58. Set Retrieval = CreateObject("Microsoft.XMLHTTP")    
  59. With Retrieval    
  60.     .Open "Get", url, False ', "", ""    
  61.     .Send    
  62.     GetPage = BytesToBstr(.ResponseBody)    
  63. End With    
  64. Set Retrieval = Nothing    
  65. End Function    
  66. Function BytesToBstr(body)    
  67. dim objstream    
  68. set objstream = Server.CreateObject("adodb.stream")    
  69. objstream.Type = 1    
  70. objstream.Mode =3    
  71. objstream.Open    
  72. objstream.Write body    
  73. objstream.Position = 0    
  74. objstream.Type = 2    
  75. objstream.Charset = "GB2312"    
  76. BytesToBstr = objstream.ReadText    
  77. objstream.Close    
  78. set objstream = nothing    
  79. End Function    
  80. on error resume next    
  81. Url="http://www.sina.com.cn"'要读取的页面地址    
  82. response.write "开始更新首页..."    
  83. wstr = GetPage(Url)    
  84. 'response.write(wstr)    
  85. Set fs=Server.CreateObject("Scripting.FileSystemObject")    
  86. 'if not MyFile.FolderExists(server.MapPath("/html/")) then    
  87. 'MyFile.CreateFolder(server.MapPath("/html/"))'    
  88. 'end if    
  89. '要存放的页面地址    
  90. dizhi=server.MapPath("index.htm")    
  91. If (fs.FileExists(dizhi)) Then    
  92. fs.DeleteFile(dizhi)    
  93. End If    
  94. Set CrFi=fs.CreateTextFile(dizhi)    
  95. Crfi.Writeline(wstr)    
  96. set CrFi=nothing    
  97. set fs=nothing    
  98. response.write "...<font color=red>更新完成!</font>"    
  99. %>  

代码算是最简单的,直接保存成一个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文件!

ASP/Visual Basic代码
  1. <%    
  2. filename="../index.html"    
  3. if request("body")<>"" then    
  4. set fso = Server.CreateObject("Scripting.FileSystemObject")    
  5. set fout = fso.CreateTextFile(server.mappath(""&filename""))    
  6. fout.write request.form("body")    
  7. fout.close    
  8. set fout=nothing    
  9. set fso=nothing    
  10. end if    
  11. %>  

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

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

ASP/Visual Basic代码
  1. '--------------------------------    
  2. '自动创建指定的多级文件夹    
  3. 'strPath为绝对路径    
  4. Function AutoCreateFolder(strPath) ’ As Boolean    
  5.          On Error Resume Next    
  6.          Dim astrPath, ulngPath, i, strTmpPath    
  7.          Dim objFSO    
  8.          If InStr(strPath, "\") <=0 Or InStr(strPath, ":") <= 0 Then   
  9.                  AutoCreateFolder = False   
  10.                  Exit Function   
  11.          End If   
  12.          Set objFSO = Server.CreateObject("Scripting.FileSystemObject")   
  13.          If objFSO.FolderExists(strPath) Then   
  14.                  AutoCreateFolder = True   
  15.                  Exit Function   
  16.          End If   
  17.          astrPath = Split(strPath, "\")   
  18.          ulngPath = UBound(astrPath)   
  19.          strTmpPath = " 
  20.          For i = 0 To ulngPath   
  21.                  strTmpPath = strTmpPath & astrPath(i) & "\ 
  22.                  If Not objFSO.FolderExists(strTmpPath) Then   
  23.                          ’ 创建   
  24.                          objFSO.CreateFolder(strTmpPath)   
  25.                  End If   
  26.          Next   
  27.          Set objFSO = Nothing   
  28.          If Err = 0 Then   
  29.                  AutoCreateFolder = True   
  30.          Else   
  31.                  AutoCreateFolder = False   
  32.          End If   
  33. End Function 调用方法:  
  34.  
  35. MyPath = "C:\a\b\c\ 
  36. If AutoCreateFolder(MyPath) Then   
  37.          Response.Write "创建文件夹成功 
  38. Else   
  39.          Response.Write "创建文件夹失败"    
  40. End If  

 

  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Arwen Build 90619 Code detection by Codefense

Copyright 2009 Your WebSite. Some Rights Reserved.