详解xmlhttp组件获取远程文件并筛选出目标数据
16lz
2021-01-22
详解xmlhttp组件获取远程文件并筛选出目标数据
getfile.asp
<%''利用xmlhttp组件获取远程文件保存到当前空间''此是网站建设中的一个实例,远程获取一个网页内容并筛选出相关的天气数据,当然可以跳过文件本地存储再获取数据''参考了xoyu的函数,在此感谢fileurl="http://www.hbqx.gov.cn/other/tqyb/inc_city_hb.asp"dotloc=InStrRev(fileurl,".")filepath="thistest"&mid(fileurl,dotloc) ''建立同类型文件名''filepath="thistest.htm" call saveRemoteFile(filepath,fileurl) sub SaveRemoteFile(LocalFileName,RemoteFileUrl)dim Ads,Retrieval,GetRemoteDataSet Retrieval = Server.CreateObject("Microsoft.XMLHTTP")With Retrieval.Open "Get", RemoteFileUrl, False, "", "".SendGetRemoteData = .ResponseBody'' GetDetail = .ResponseText ''对文本型文件可直接获取内容,但不能支持中文,不知道如何解决End With Set Retrieval = 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 WithSet Ads=nothingend sub ''以上完成远程存储文件,以下只适用于对文本型文件的操作www.knowsky.comset fso=server.createobject("scripting.filesystemobject") ''读取文件内容set fileout=fso.opentextfile(server.mappath(filepath),1)content=fileout.readallset fileout=nothingset fso=nothing''response.write content contentarr=split(content,"<td") ''根据内容进行筛选for i=1 to ubound(contentarr)if instr(contentarr(i),"恩施") then thisloc=inextdim xu(5)for j=0 to 4con1=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)nextif xu(1)<>"" thenresponse.write "document.write('恩施州未来24小时天气预报:"&xu(1)&",最低温度"&xu(2)&"摄氏度,最高温度"&xu(3)&"摄氏度,风向:"&xu(4)&".--武汉中心气象台发布');"elseresponse.write "document.write('暂未发布');"end if''response.write server.htmlencode(str)%>
在另一个htm页中引用显示获取内容
<script src="getfile.asp"></script>
更多相关文章
- 教你如何打开xml文件的详解
- XML文件要有根标签(错误)的代码解决分享
- 读取XML文件时报“前言中不允许有内容”错误处理办法详解
- 解析XML文件的几种方式对比的详细介绍
- XML文件使用SAX方法读取的示例代码
- XML文件使用DOM方法读取的示例代码
- 如何打开xml文件介绍
- maven项目不编译xml文件的解决办法
- 四种获取RSS源xml文件的方法