詳解xmlhttp元件取得遠端檔案並篩選出目標數據

黄舟
發布: 2017-03-27 16:47:27
原創
2067 人瀏覽過

詳解xmlhttp元件取得遠端檔案並篩選出目標資料

getfile.asp

<%
&#39;&#39;利用xmlhttp组件获取远程文件保存到当前空间
&#39;&#39;此是网站建设中的一个实例,远程获取一个网页内容并筛选出相关的天气数据,当然可以跳过文件本地存储再获取数据
&#39;&#39;参考了xoyu的函数,在此感谢
fileurl="http://www.hbqx.gov.cn/other/tqyb/inc_city_hb.asp"
dotloc=InStrRev(fileurl,".")
filepath="thistest"&mid(fileurl,dotloc) &#39;&#39;建立同类型文件名
&#39;&#39;filepath="thistest.htm"
   
call saveRemoteFile(filepath,fileurl)
   
sub SaveRemoteFile(LocalFileName,RemoteFileUrl)
dim Ads,Retrieval,GetRemoteData
Set Retrieval = Server.CreateObject("Microsoft.XMLHTTP")
With Retrieval
.Open "Get", RemoteFileUrl, False, "", ""
.Send
GetRemoteData = .ResponseBody
&#39;&#39; GetDetail = .ResponseText &#39;&#39;对文本型文件可直接获取内容,但不能支持中文,不知道如何解决
End With
   
Set Retrieval = Nothing
&#39;&#39;RESPONSE.WRITE GetDetail
   
Set Ads = Server.CreateObject("Adodb.Stream") &#39;&#39;生成对应文件
With Ads
.Type = 1
.Open
.Write GetRemoteData
.SaveToFile server.MapPath(LocalFileName),2
.Cancel()
.Close()
End With
Set Ads=nothing
end sub
   
&#39;&#39;以上完成远程存储文件,以下只适用于对文本型文件的操作www.knowsky.com
set fso=server.createobject("scripting.filesystemobject") &#39;&#39;读取文件内容
set fileout=fso.opentextfile(server.mappath(filepath),1)
content=fileout.readall
set fileout=nothing
set fso=nothing
&#39;&#39;response.write content
   
contentarr=split(content,"<td") &#39;&#39;根据内容进行筛选
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(&#39;恩施州未来24小时天气预报:"&xu(1)&",最低温度"&xu(2)&"摄氏度,最高温度"&xu(3)&"摄氏度,风向:"&xu(4)&".--武汉中心气象台发布&#39;);"
else
response.write "document.write(&#39;暂未发布&#39;);"
end if
&#39;&#39;response.write server.htmlencode(str)
%>
登入後複製

在另一個htm頁中引用顯示取得內容

<script src="getfile.asp"></script>
登入後複製

以上是詳解xmlhttp元件取得遠端檔案並篩選出目標數據的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!