用XML組件生成靜態(tài)首頁
來源:易賢網(wǎng) 閱讀:1061 次 日期:2014-09-10 16:45:09
溫馨提示:易賢網(wǎng)小編為您整理了“用XML組件生成靜態(tài)首頁”,方便廣大網(wǎng)友查閱!

第 1 頁 利用Msxml2.ServerXMLHTTP抓取網(wǎng)頁內(nèi)容

第 2 頁 用ADODB.Stream將抓取內(nèi)容寫入文件

第 3 頁 具體的舉一個(gè)例子

了解asp的人應(yīng)該都知道asp是一種解釋執(zhí)行的腳本程序語言,而腳本程序的執(zhí)行效率往往都是很低的,如果站點(diǎn)的訪問量相對(duì)較高的話服務(wù)器就會(huì)非常消耗資源,表現(xiàn)的結(jié)果就是站點(diǎn)訪問速度急速下降.解決的方法,除了優(yōu)化程序提高執(zhí)行效率,還有一個(gè)方法就是將網(wǎng)站內(nèi)的訪問量大的頁面定時(shí)的生成靜態(tài)html文件,這樣可以非常有效的解決訪問速度問題,當(dāng)然前提是你的服務(wù)器速度也要不是很慢了,不然怎么弄都是沒有效果的. 下面我介紹一種利用Msxml2.ServerXMLHTTP組件來抓取您所要生成靜態(tài)的網(wǎng)頁,然后再利用fso,或者ado來寫入文件的一種方法,需要注意的是本文例子全部采用utf-8編碼,如果改為gb2312需要做相應(yīng)屬性的修改! 先給處下面的函數(shù):

<!--'相關(guān)問題可訪問

Function GetURL(URL)

'下載主函數(shù)

const TimeInterval=60

'設(shè)定時(shí)間間隔

'如果下載時(shí)間很慢,就寫成120秒

'Response.LCID=2052

const lResolve=6

'解析域名超時(shí)時(shí)間,秒

const lConnect=6

'連接站點(diǎn)超時(shí)時(shí)間,秒

const lSend=6

'發(fā)送數(shù)據(jù)請(qǐng)求超時(shí)時(shí)間,秒

const lReceive=40

'下載數(shù)據(jù)超時(shí)時(shí)間,秒

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="對(duì)象初始化失敗"

Err.Clear

set http=nothing

Exit Function

Case 1

GetURL="域名分析超時(shí)/連接站點(diǎn)超時(shí)"

Err.Clear

set http=nothing

Exit Function

Case 2

GetURL="發(fā)送數(shù)據(jù)請(qǐng)求超時(shí),是不是服務(wù)器出故障了"

Err.Clear

set http=nothing

Exit Function

Case 3

GetURL="數(shù)據(jù)下載超時(shí)/等待反饋時(shí)間超時(shí)"

Err.Clear

set http=nothing

Exit Function

Case 4

'下載成功

End Select

If http.status<>200 then

GetURL="下載失敗"&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

-->

主要功能是抓取地址參數(shù)的網(wǎng)頁文件的內(nèi)容使用方法varia=GetURL(""),如果是本地測(cè)試地址可以寫成使用此函數(shù)需要注意的是Response.LCID=2052屬性在windows server 2000下不被支持,不過問題不大只要注釋掉即可正常使用! 還有一些超時(shí)屬性可以根據(jù)需要自定義,但注意不要設(shè)置的時(shí)間太短,否則如果文件大或者地址訪問速度較慢就容易抓取失敗! 這讓我們就可以利用此函數(shù)來抓取你想要生成的網(wǎng)頁文件內(nèi)容了.將內(nèi)容存入變量,等著寫入文件吧!

下面給出這個(gè)類,用來將剛剛利用函數(shù)抓取的內(nèi)容寫入相應(yīng)文件,這樣就大功告成了! 直接生成你所要生成的網(wǎng)頁吧,非常方便而且不用修改原來的文件!

Class Htmlmaker

'相關(guān)問題請(qǐng)參看

'/*************************

'/ 屬性設(shè)置說明

'/ foldename "文件夾名"

'/ 如果不設(shè)置,將自動(dòng)生成[年月日]時(shí)間格式的文件夾名

'/ Filename "文件名"(含前后綴)

'/ 如果不設(shè)置,將自動(dòng)生成[時(shí)分秒]時(shí)間格式的文件名,后綴為.html

'/ Htmlstr "生成的代碼內(nèi)容"

'/*************************

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

'/*************************

'/ 文件名轉(zhuǎn)換日期函數(shù)

'/*************************

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

'/*************************

'/ 初試化

'/*************************

Private Sub class_initialize()

HtmlFolder=Datename1(now)

HtmlFilename=Datename2(now)&".html"

HtmlC

End Sub

Private Sub class_terminate()

End Sub

'/*************************

'/ Html文件生成

'/*************************

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文件刪除

'/*************************

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

下面為了讓大家更好的學(xué)習(xí)給出具體的舉一個(gè)實(shí)例:

我們有一個(gè)網(wǎng)站地址是

我們要將其首頁也就是default.asp生成靜態(tài)的htm文件

我們先建立個(gè)文件:makeindex.asp

<!--#include file="function_class.asp"-->

<%

dim indexhtmlstr

indexhtmlstr=GetURL("")

dim indexfilename

indexfilename="index.htm"

dim actionstat

if len(indexhtmlstr) <200 then

acti&indexfilename&"文件時(shí)遇到"&indexhtmlstr&"錯(cuò)誤"

else

dim myhtml

set myhtml= new Htmlmaker

myhtml.foldename = "../.."

myhtml.Filename = indexfilename

myhtml.Htmldel

myhtml.Htmlstr = indexhtmlstr

myhtml.Htmlmake

set myhtml=nothing

acti&indexfilename&"文件"

end if

response.write actionstat

%>

文件function_class.asp的內(nèi)容主要包括前面給出的函數(shù)和生成文件的類就可以了!

運(yùn)行makeindex.asp就可以生成htm文件了!

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:用XML組件生成靜態(tài)首頁
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)