ASP获取json天气信息

时间:2023-03-09 17:31:14
ASP获取json天气信息

ASP代码(ASP获取页面源码方法,有编码、超时时间参数,处理了乱码、超时的问题):

Function GetHttpPage(HttpUrl)
If IsNull(HttpUrl)=True Or Len(HttpUrl)< Then
GetHttpPage="$False$"
Exit Function
End If
Dim Http
'Set Http=server.createobject("MSXML2.XMLHTTP")
Set Http=server.createobject("MSxml2.ServerxmlHTTP")
Http.SetTimeOuts ,,,
Http.open "GET",HttpUrl,False
Http.Send()
If Http.Readystate<> then
Set Http=Nothing
GetHttpPage="False"
Exit function
End if
GetHTTPPage=BytesToBstr(Http.responseBody,"UTF-8")
Set Http=Nothing
If Err.number<> then
Err.Clear
End If
End Function Function BytesToBstr(Body,Cset)
Dim Objstream
Set Objstream = Server.CreateObject("adodb.stream")
objstream.Type =
objstream.Mode =
objstream.Open
objstream.Write body
objstream.Position =
objstream.Type =
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function

ASP方法使用(从m.weather.com.cn获取上海天气json数据):

<%
html=GetHttpPage("http://m.weather.com.cn/data/101020100.html")
'response.write(html)
%>

JS脚本将获取的数据追加到页面显示(用到了ASP和jQuery)

<script type="text/javascript">
var weather=eval(<%=html%>);
var htmlInfo="";
var dateVal="<%=year(now()) %>年<%=month(now()) %>月<%=day(now()) %>日"; if(weather!=undefined)
{
if(dateVal!=weather.weatherinfo.date_y)
{
htmlInfo="暂时无法获取最新上海天气数据&nbsp;";
}
else
{
htmlInfo=weather.weatherinfo.date_y+"&nbsp;"+
weather.weatherinfo.week+
"&nbsp;上海天气&nbsp;"+weather.weatherinfo.img_title1+
"&nbsp;气温"+weather.weatherinfo.temp1+"&nbsp;"+
weather.weatherinfo.wind1+"&nbsp;"+
"紫外线“"+weather.weatherinfo.index_uv+"”&nbsp;"+
weather.weatherinfo.index_d;
}
}
else
{
htmlInfo="暂时无法获取上海天气数据&nbsp;"
}
$("#weatherSpan").html(htmlInfo);
</script>

参考:

asp获取网页源码函数

中国国家气象局天气预报信息接口

之后做的调整:

1. GetHttpPage方法中SetTimeOuts 2000,2000,2000,2000是后来补充的,设置了超时时间。

同时原Set Http=server.createobject("MSXML2.XMLHTTP")也改成了Set Http=server.createobject("MSxml2.ServerxmlHTTP")。

参数说明参考:http://www.zzsky.cn/build/content/1427.htm

2. 原weather.weatherinfo!=undefined的判断也不对,已经更新成weather!=undefined。

3. 增加ASP缓存处理,缓存天气信息:

ASP缓存关键代码(使用的Application,所以是全局共享缓存——参考):

CONST_CACHE_DEFAULT_INTERVAL =  '每隔1小时刷新一次cache

Function IsExpires(cacheName)
Dim strLastUpdate
strLastUpdate= Application(cacheName & "_LastUpdate")
If ((strLastUpdate = "") Or (CONST_CACHE_DEFAULT_INTERVAL < DateDiff("s", strLastUpdate, Now))) Then
ClearCacheData cacheName
IsExpires=true
Else
IsExpires=false
End If
End Function Sub ClearCacheData(cacheName)
Application.Lock
Application(cacheName) = ""
Application(cacheName & "_LastUpdate") = ""
Application.UnLock
End Sub Sub SetCacheData(cacheName,content)
Application.Lock
Application(cacheName) = content
Application(cacheName & "_LastUpdate") = CStr(now())
Application.UnLock
End Sub Function GetCacheData(cacheName)
If (IsExpires(cacheName)) Then
ClearCacheData cacheName
End If
GetCacheData=Application(cacheName)
End Function

使用:

JS转json的方法:

<script language="JScript" runat="Server">
function toObject(json)
{
eval("var o=" + json);
return o;
}
</script>

vbscript代码(增加了是否是当天天气的判断,使用js的方法处理json):

htmlWeather=GetCacheData("htmlWeatherInfo")
if(htmlWeather="") then
htmlWeather=GetHttpPage("http://113.108.239.107/data/101020100.html")'http://m.weather.com.cn/data/101020100.html
SetCacheData "htmlWeatherInfo",htmlWeather
'response.Write Application("htmlWeatherInfo_LastUpdate")
else
objVal=toObject(htmlWeather)
if objVal<>"" and objVal.weatherinfo.date_y<>(year(now())&"年"&month(now())&"月"&day(now())&"日") then
htmlWeather=GetHttpPage("http://113.108.239.107/data/101020100.html")'http://m.weather.com.cn/data/101020100.html
SetCacheData "htmlWeatherInfo",htmlWeather
end if
'response.Write toObject(htmlWeather).weatherinfo.date_y
'response.Write Application("htmlWeatherInfo_LastUpdate")
end if

4.如果出现http://m.weather.com.cn/data/101020100.html在实际环境无法访问可尝试换成IP地址的URL:http://113.108.239.107/data/101020100.html

5.其他天气接口页面:

http://m.weather.com.cn/m/pn1/weather.htm?id=101200101T 这个链接有15种不同格式的,从pn1到pn15,ID是需要查询的那个城市的代码(城市代码参考:中国国家气象局天气预报信息接口

http://m.weather.com.cn/m/pn1/weather.htm 这个链接有15种不同格式的,从pn1到pn15

http://m.weather.com.cn/m/p1/weather1.htm  这个链接有13种不同格式的,从p1到p13

http://m.weather.com.cn/n/pn1/weather.htm 这个链接有14种不同格式的,从pn1到pn14

http://www.weather.com.cn/static/custom/custom.html 这个链接可以同时显示多个城市的天气