Page 1 uses Msxml2.ServerXMLHTTP to capture web content. Page 2 uses ADODB.Stream to write the captured content into a file. Page 3 gives a specific example. Anyone who knows asp should know that asp is a scripting language that interprets and executes. , and the execution efficiency of script programs is often very low. If the number of visits to the site is relatively high, the server will consume a lot of resources. The result is that the site access speed drops rapidly. The solution is to optimize the program to improve execution efficiency. , and another method is to regularly generate static html files for highly visited pages in the website, which can very effectively solve the access speed problem. Of course, the premise is that your server speed is not very slow, otherwise it will not work no matter what. It has no effect. Below I introduce a method of using the Msxml2.ServerXMLHTTP component to grab the static web page you want to generate, and then using fso or ado to write the file. It should be noted that the examples in this article all use utf -8 encoding, if it is changed to gb2312, the corresponding attributes need to be modified! First give the following function:
<!--'For related questions, please visit http://www.downcodes.com
Function GetURL(URL)
'Download main function
const TimeInterval=60
'Set time interval
'If the download time is very slow, write it as 120 seconds
'Response.LCID=2052
const lResolve=6
'Resolution domain name timeout, seconds
const lConnect=6
'Connection site timeout, seconds
const lSend=6
'Send data request timeout, seconds
const lReceive=40
'Download data timeout, seconds
on error resume Next
Dim http
Set http = Server.CreateObject("Msxml2.ServerXMLHTTP")
http.setTimeouts lResolve*1000,lConnect*1000,lSend*1000,lReceive*1000
http.Open "GET",URL,False
http.Send
Select Case http.readyState
Case 0
GetURL="Object initialization failed"
Err.Clear
set http=nothing
Exit Function
Case 1
GetURL="Domain name analysis timeout/connection site timeout"
Err.Clear
set http=nothing
Exit Function
Case 2
GetURL="The data request timed out. Is the server faulty?"
Err.Clear
set http=nothing
Exit Function
Case 3
GetURL="Data download timeout/waiting feedback timeout"
Err.Clear
set http=nothing
Exit Function
Case 4
'Download successful
End Select
If http.status<>200 then
GetURL="Download failed"&Err.description
Err.Clear
set http=nothing
Exit Function
END IF
If http.status="200" then
GetURL=http.ResponseText
'GetURL=SaveFile()
End If
set http=nothing
End Function
-->
The main function is to capture the content of the web page file of the address parameter. Use the method varia=GetURL(" http://www.downcodes.com "). If it is a local test address, you can write it as http://localhost/default.asp and use this function. It should be noted that the Response.LCID=2052 attribute is not supported under Windows Server 2000, but it is not a big problem and can be used normally as long as it is commented out! There are also some timeout attributes that can be customized as needed, but be careful not to set the time too short , otherwise if the file is large or the address access speed is slow, the crawling may fail! This allows us to use this function to crawl the content of the web page file you want to generate. Store the content in a variable and wait to be written into the file. !
This class is given below, which is used to write the content just captured by the function into the corresponding file, and you're done! Directly generate the web page you want to generate, it is very convenient and does not need to modify the original file!
Class Htmlmaker
'Please ask for related questions See http://www.downcodes.com
'/************************
'/Property setting instructions
'/foldename "folder name"
'/ If not set, a folder name in the [year, month, day] time format will be automatically generated
'/ Filename "File name" (including suffixes and suffixes)
'/ If not set, a file name in [hours, minutes, seconds] time format will be automatically generated, with the suffix .html
'/ Htmlstr "Generated code content"
'/************************
Private HtmlFolder,HtmlFilename,HtmlContent
Public property let foldename(str)
HtmlFolder=str
End property
Public property let Filename(str)
HtmlFilename=str
End property
Public property let Htmlstr(str)
HtmlContent=str
End property
'/************************
'/File name conversion date function
'/***********************
Private Function Datename1(timestr)
dim s_year,s_month,s_day
s_year=year(timestr)
if len(s_year)=2 then s_year="20"&s_year
s_month=month(timestr)
if s_month<10 then s_month="0"&s_month
s_day=day(timestr)
if s_day<10 then s_day="0"&s_day
Datename1=s_year & s_month & s_day
End Function
Private Function Datename2(timestr)
dim s_hour,s_minute,s_ss
s_hour=hour(timestr)
if s_hour<10 then s_hour="0"&s_hour
s_minute=minute(timestr)
if s_minute<10 then s_minute="0"&s_minute
s_ss=second(timestr)
if s_ss<10 then s_ss="0"&s_ss
Datename2 = s_hour & s_minute & s_ss
End Function
'/*************************
'/ initialization
'/***********************
Private Sub class_initialize()
HtmlFolder=Datename1(now)
HtmlFilename=Datename2(now)&".html"
HtmlC
End Sub
Private Sub class_terminate()
End Sub
'/************************
'/Html file generation
'/**********************
Public Sub Htmlmake()
'On Error Resume Next
dim filepath,fso,fout
filepath = HtmlFolder&"/"&HtmlFilename
Set fso = Server.CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(Server.MapPath(HtmlFolder)) Then
Else
fso.CreateFolder(Server.MapPath(HtmlFolder))
End If
' Set fout = fso.CreateTextFile(Server.MapPath(filepath),true)
' fout.WriteLine HtmlContent
'fout.close
dim objFSO,adTypeText,adSaveCreateOverWrite,Charsett,objAdoStream
Charsett = "utf-8"
set objAdoStream = Server.CreateObject("ADODB.Stream")
adTypeText = 2
adSaveCreateOverWrite = 2
objAdoStream.Type = adTypeText
objAdoStream.Open
objAdoStream.Charset = Charsett
objAdoStream.WriteText(HtmlContent)
objAdoStream.SaveToFile Server.MapPath(filepath),2
objAdoStream.Close
End Sub
'/***********************
'/Html file delete
'/************************
Public Sub Htmldel()
dim filepath,fso
filepath = HtmlFolder&"/"&HtmlFilename
Set fso = CreateObject("Scripting.FileSystemObject")
if fso.FileExists(Server.MapPath(filepath)) then
fso.DeleteFile(Server.mappath(filepath))
end if
Set fso = nothing
End Sub
End class
In order to let everyone learn better, let's give a specific example:
We have a website address http://www.downcodes.com/.
We want to generate a static html file from its homepage, which is default.asp.
We first create it. File: makeindex.asp
<!--#include file="function_class.asp"-->
<%
dim indexhtmlstr
indexhtmlstr=GetURL(" http://www.downcodes.com/default.asp ")
dim indexfilename
indexfilename="index.htm"
dim actionstat
if len(indexhtmlstr) <200 then
Acti&indexfilename&"encountered "&indexhtmlstr&" error"
else
dim myhtml
set myhtml= new Htmlmaker
myhtml.foldename = "../.."
myhtml.Filename = indexfilename
myhtml.Htmldel
myhtml.Htmlstr = indexhtmlstr
myhtml.Htmlmake
set myhtml=nothing
acti&indexfilename&"file"
end if
response.write actionstat
%>
The content of the file function_class.asp mainly includes the functions given above and the class that generates the file!
Run makeindex.asp to generate the htm file!