免费天气插件asp版本

系统 1629 0

天气信息来源:中国天气网【weather.com.cn】
根据QQ[http://fw.qq.com:80/ipaddress]得到ip地址和所在城市信息,
然后调用weather.com.cn网站的xml得到城市代码,就可以得到JSON格式的天气信息
setWeather函数可以自行修改生成您想要的天气信息格式

效果如下:
免费天气插件asp版本

Weather.asp
    
<%codepage=936%>
<%
'***********************************************************
' 文件名: 免费天气插件asp版本,仅适用于中国用户
' 版本:  mmWeather1.0 
' 作者:  走过四季 
' 电子邮件: maomaoysq@sohu.com 
' 日期:  2010年01月14日
' 功能: 根据用户的ip地址自动获取所在城市的天气信息
' 声明:   
'   本代码可以自由使用,但请保留此版权声明信息 
'   如果您对本代码进行修改增强, 
'   请发送一份给俺。 
'**********************************************************

dim cityid,city1,city2,tmpHtml,tmpArr,tLevel,weaXML,weaHTML
tLevel = 0
weaXML = "http://service.weather.com.cn/plugin/"
weaHTML = "http://m.weather.com.cn/data/"
cityid = Trim(Request.Cookies("wea_cityid"))

if cityid = "" then
	tmpHtml = GetUrlBody("http://fw.qq.com:80/ipaddress","gb2312")
	tmpHtml = BytesToBstr(tmpHtml,"gb2312")
	if tmpHtml = "" then
		Response.Write("Error:Can't get ip address from qq.com.")
	end if
	tmpArr = Split(tmpHtml,",")
	Response.Cookies("wea_cityip") = tmpArr(0)
	city1 = tmpArr(2)
	city2 = tmpArr(3)
	city1 = Replace(city1,"省","")
	city1 = Replace(city1,"市","")
	city1 = Replace(city1,"""","")
	city2 = Replace(city2,");","")

	if Trim(city2) = "" then
		city2 = city1
	else
		city2 = Replace(city2,"市","")
		city2 = Replace(city2,"自治区","")
	end if
	city2 = Replace(city2,"""","")
	'call getCityCode(city2)
	if cityid = "" then
		call getLocalCity("data/city.xml",0)
	end if
else
	call getWeather(cityid)
end if

function getLocalCity(ByVal tUrl,ByVal tLevel)
	dim iPos,iPos1,cid,cArr
	tmpHtml = GetUrlBody(weaXML & tUrl,"")
	
	iPos = InStr(tmpHtml,city1)
	iPos1 = InStr(tmpHtml,city2)
	if tLevel<3 then
		if iPos>0 then
			tmpHtml = Left(tmpHtml,iPos-1)
			iPos = InStrRev(tmpHtml,",")
			tmpHtml = Right(tmpHtml,len(tmpHtml)-iPos)
			cid = Replace(tmpHtml,"|",""):
		elseif iPos1>0 then
			tmpHtml = Left(tmpHtml,iPos1-1)
			iPos = InStrRev(tmpHtml,",")
			tmpHtml = Right(tmpHtml,len(tmpHtml)-iPos)
			cid = Replace(tmpHtml,"|",""):
		end if
		call getLocalCity("data/city" & cid & ".xml",tLevel+1)
	else
		cid = Split(tmpHtml,"|")(1)
		call getWeather(cid)
	end if
end function

function getWeather(ByVal cid)
	Response.Cookies("wea_cityid") = cid
	tmpHtml = GetUrlBody(weaHTML & cid & ".html","")
	if tmpHtml="" then
		Response.Write("Error:Nothing is from "&weaHTML & cid & ".html")
	else
		Response.Write("<script language=javascript>"& vbCrLf &"function window.onload(){var oJson = "& tmpHtml & vbCrLf)
		Response.Write("setWeather(oJson);}"& vbCrLf &"</script>")
	end if
end function

Function BytesToBstr(body,Cset)
	dim objstream
	set objstream = Server.CreateObject("adodb.stream")
	objstream.Type = 1
	objstream.Mode =3
	objstream.Open
	objstream.Write body
	objstream.Position = 0
	objstream.Type = 2
	objstream.Charset = Cset
	BytesToBstr = objstream.ReadText
	objstream.Close
	set objstream = nothing
End Function 

Public Function GetUrlBody(ByVal URL,ByVal CharSet)
	On Error Resume Next
	Set Http = Server.CreateObject("MICROSOFT.XMLHTTP")
	Http.Open "GET", URL, False
	Http.Send
	If Http.Readystate = 4 Then
		If Http.Status = 200 Then
			if CharSet="gb2312" then
			GetUrlBody = Http.responseBody
			else
			GetUrlBody = Http.responseText
			end if
		End If
	End If
End Function

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>中国天气网-天气预报</title>
</head>
<body>
<div class="Weather">
	<span id="city"></span>&nbsp;<span id="temp1"></span>&nbsp;<span id="wd1"></span>
</div>
</body>
</html>
<script language=javascript>
function setWeather(objJson)
{
	var imgHtml = "";
	var cityname = objJson.weatherinfo.city;		//上海
	var id = objJson.weatherinfo.cityid;			//101020100 
	var cityinfo1 = objJson.weatherinfo.weather1;	//晴转多云
	var wd1 = objJson.weatherinfo.wind1;			//北风3-4级
	var fl1 = objJson.weatherinfo.fl1;			//3-4级
	var temp1 = objJson.weatherinfo.temp1;		//4℃~-1℃

	//注释里面为更详细的信息,您可以根据自己需要使用
	/*var cityinfo2=objJson.weatherinfo.weather2;
	var wd2=objJson.weatherinfo.wind2;
	var fl2=objJson.weatherinfo.fl2;
	var temp2=objJson.weatherinfo.temp2;
	var img1=objJson.weatherinfo.img1;
	var img2=objJson.weatherinfo.img2;
	var img3=objJson.weatherinfo.img3;
	var img4=objJson.weatherinfo.img4;
	var index=objJson.weatherinfo.index;
	var index_d=objJson.weatherinfo.index_d;
	var index_xc=objJson.weatherinfo.index_xc;
	var index_uv=objJson.weatherinfo.index_uv;
	var date=objJson.weatherinfo.date;
	var date_y=objJson.weatherinfo.date_y;
	var imgtitle1=objJson.weatherinfo.img_title1;
	var imgtitle2=objJson.weatherinfo.img_title2;
	var imgsingle=objJson.weatherinfo.img_single;
	var imgtitlesingle=objJson.weatherinfo.img_title_single;
	*/

	document.all.city.innerHTML = cityname
	document.all.temp1.innerHTML = cityinfo1
	document.all.wd1.innerHTML = wd1 + fl1
}
</script>

  

免费天气插件asp版本


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论