getfile.asp
1
2''利用xmlhttp组件获取远程文件保存到当前空间
3''此是网站建设中的一个实例,远程获取一个网页内容并筛选出相关的天气数据,当然可以跳过文件本地存储再获取数据
4''参考了xoyu的函数,在此感谢
5fileurl=" http://www.hbqx.gov.cn/other/tqyb/inc_city_hb.asp "
6dotloc=InStrRev(fileurl,".")
7filepath="thistest"∣(fileurl,dotloc) ''建立同类型文件名
8''filepath="thistest.htm"
9
10call saveRemoteFile(filepath,fileurl)
11
12sub SaveRemoteFile(LocalFileName,RemoteFileUrl)
13dim Ads,Retrieval,GetRemoteData
14Set Retrieval = Server.CreateObject("Microsoft.XMLHTTP")
15With Retrieval
16.Open "Get", RemoteFileUrl, False, "", ""
17.Send
18GetRemoteData = .ResponseBody
19'' GetDetail = .ResponseText ''对文本型文件可直接获取内容,但不能支持中文,不知道如何解决
20End With
21
22Set Retrieval = Nothing
23''RESPONSE.WRITE GetDetail
24
25Set Ads = Server.CreateObject("Adodb.Stream") ''生成对应文件
26With Ads
27.Type = 1
28.Open
29.Write GetRemoteData
30.SaveToFile server.MapPath(LocalFileName),2
31.Cancel()
32.Close()
33End With
34Set Ads=nothing
35end sub
36
37''以上完成远程存储文件,以下只适用于对文本型文件的操作 www.knowsky.com
38set fso=server.createobject("scripting.filesystemobject") ''读取文件内容
39set fileout=fso.opentextfile(server.mappath(filepath),1)
40content=fileout.readall
41set fileout=nothing
42set fso=nothing
43''response.write content
44
45contentarr=split(content,"
<td") ''根据内容进行筛选="" 4="" con1="contentarr(thisloc+j)" dim="" for="" i="1" if="" instr(contentarr(i),"恩施")="" j="0" next="" start1='instr(con1,"' then="" thisloc="i" to="" ubound(contentarr)="" xu(5)="">")
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('恩施州未来24小时天气预报:"&xu(1)&",最低温度"&xu(2)&"摄氏度,最高温度"&xu(3)&"摄氏度,风向:"&xu(4)&".--武汉中心气象台发布');"
else
response.write "document.write('暂未发布');"
end if
''response.write server.htmlencode(str)
1
2在另一个htm页中引用显示获取内容
3<script src="getfile.asp"></script></td")>