xmlhttp組(zǔ)件獲取(qǔ)遠程文件並篩選出目標數據
getfile.asp
<%
''利用xmlhttp組件獲取遠程文件保(bǎo)存到當前空間
''此是(shì)網站建設(shè)中的(de)一(yī)個實例,遠程獲取(qǔ)一個網(wǎng)頁內容並(bìng)篩選出相關的天氣數據,當然可以跳過文件本(běn)地(dì)存儲再獲取數據
''參考了(le)xoyu的函數,在此感謝
fileurl="http://www.hbqx.gov.cn/other/tqyb/inc_city_hb.asp"
dotloc=InStrRev(fileurl,".")
filepath="thistest"&mid(fileurl,dotloc) ''建(jiàn)立同類型文(wén)件名
''filepath="thistest.htm"
call saveRemoteFile(filepath,fileurl)
sub SaveRemoteFile(LocalFileName,RemoteFileUrl)
dim Ads,Retrievals,GetRemoteData
Set Retrievals = Server.CreateObject("Microsoft.XMLHTTP")
With Retrievals
.Open "Get", RemoteFileUrl, False, "", ""
.Send
GetRemoteData = .ResponseBody
'' GetDetail = .ResponseText ''對文(wén)本型文件可直接獲取內容,但不能支持中文,不知道如何解決
End With
Set Retrievals = Nothing
''RESPONSE.WRITE GetDetail
Set Ads = Server.CreateObject("Adodb.Stream") ''生成對應文件
With Ads
.Type = 1
.Open
.Write GetRemoteData
.SaveToFile server.MapPath(LocalFileName),2
.Cancel()
.Close()
End With
Set Ads=nothing
end sub
''以上完成遠程存(cún)儲文件,以下隻適(shì)用於對文本型文件的操作(zuò)
set fso=server.createobject("scripting.filesystemobject") ''讀取文件內容
set fileout=fso.opentextfile(server.mappath(filepath),1)
content=fileout.readall
set fileout=nothing
set fso=nothing
''response.write content
contentarr=split(content,"<td") ''根據內容進行篩選
for i=1 to ubound(contentarr)
if instr(contentarr(i),"恩施") then thisloc=i
next
dim xu(5)
for j=0 to 4
con1=contentarr(thisloc+j)
start1=instr(con1,">")
con1=right(con1,len(con1)-start1)
stop1=instr(con1,"<")
con1=left(con1,stop1-1)
str=str&contentarr(thisloc+j)
xu(j)=trim(con1)
next
if xu(1)<>"" then
response.write "document.write('恩施州(zhōu)未來24小時天氣預報(bào):"&xu(1)&",最低溫度(dù)"&xu(2)&"攝氏(shì)度,最(zuì)高溫度"&xu(3)&"攝氏度,風向:"&xu(4)&".--武漢中心氣象台發布');"
else
response.write "document.write('暫未發布');"
end if
''response.write server.htmlencode(str)
%>
在另一個htm頁中引用顯示獲取內容(róng)
<script src="getfile.asp"></script>
關鍵詞:xmlhttp組件(jiàn)
閱讀本文後您有什麽感想? 已有 人(rén)給出評(píng)價!
- 1
- 1
- 1
- 1
- 1
- 1