DBMNG数据库管理与应用

抓住自己最有兴趣的东西,由浅入深,循序渐进地学……
当前位置:首页 > 服务器配置 > IIS&ASP

ASP正则式获取天气代码

<%'@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%'Response.CodePage="65001"%>  
<%'Response.Charset="UTF-8" %>
<% 
Response.Expires = -9999 
Response.AddHeader "Pragma","no-cache" 
Response.AddHeader "cache-ctrol","no-cache" 
Function GetBody(weburl) 
Dim ObjXMLHTTP 
Set ObjXMLHTTP=Server.CreateObject("MSXML2.serverXMLHTTP") 
ObjXMLHTTP.Open "GET",weburl,False 
ObjXMLHTTP.send 
While ObjXMLHTTP.readyState <> 4 
ObjXMLHTTP.waitForResponse 10000 
Wend 
GetBody=ObjXMLHTTP.responseBody 
Set ObjXMLHTTP=Nothing 
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 

Function RemoveDiv(strHTML) 
Dim objRegExp, Match, Matches ,tmpstr
	Set objRegExp = New Regexp 
	objRegExp.IgnoreCase = True 
	objRegExp.Global = True 
	
	objRegExp.Pattern = "<div>&nbsp;天气:.+?</div>" '取闭合的<div></div>>
	Set Matches = objRegExp.Execute(strHTML) '进行匹配 
	For Each Match in Matches ' 遍历匹配集合
		tmpstr=tmpstr+""+Match.Value
	Next 
	tmpstr=replace(tmpstr,"<div>&nbsp;天气:","")
	tmpstr=replace(tmpstr,"</div>","")+"&nbsp;&nbsp;"
	
	objRegExp.Pattern = "<div>&nbsp;温度:.+?</div>" '取闭合的<div></div>>
	Set Matches = objRegExp.Execute(strHTML) '进行匹配 
	For Each Match in Matches ' 遍历匹配集合
		tmpstr=tmpstr+"实时温度:"+Match.Value
	Next 
	tmpstr=replace(tmpstr,"<div>&nbsp;温度:","")
	tmpstr=replace(tmpstr,"</div>","")+"&nbsp;&nbsp;"
	
	objRegExp.Pattern = "<div>&nbsp;体感温度:.+?</div>" '取闭合的<div></div>>
	Set Matches = objRegExp.Execute(strHTML) '进行匹配 
	For Each Match in Matches ' 遍历匹配集合
		tmpstr=tmpstr+"体感温度:"+Match.Value
	Next
	tmpstr=replace(tmpstr,"<div>&nbsp;体感温度:","")
	tmpstr=replace(tmpstr,"</div>","")+"&nbsp;&nbsp;"
	
	objRegExp.Pattern = "<div>&nbsp;舒适度:.+?</div>" '取闭合的<div></div>>
	Set Matches = objRegExp.Execute(strHTML) '进行匹配 
	For Each Match in Matches ' 遍历匹配集合
		tmpstr=tmpstr+"舒适度:"+Match.Value
	Next 
	tmpstr=replace(tmpstr,"<div>&nbsp;舒适度:","")
	tmpstr=replace(tmpstr,"</div>","")+"&nbsp;&nbsp;"
	
	objRegExp.Pattern = "<div>&nbsp;风向:.+?</div>" '取闭合的<div></div>>
	Set Matches = objRegExp.Execute(strHTML) '进行匹配 
	For Each Match in Matches ' 遍历匹配集合
		tmpstr=tmpstr+"实时风向:"+Match.Value
	Next 
	tmpstr=replace(tmpstr,"<div>&nbsp;风向:","")
	tmpstr=replace(tmpstr,"</div>","")+"&nbsp;&nbsp;"
	
	objRegExp.Pattern = "<div>&nbsp;相对湿度:.+?</div>" '取闭合的<div></div>>
	Set Matches = objRegExp.Execute(strHTML) '进行匹配 
	For Each Match in Matches ' 遍历匹配集合
		tmpstr=tmpstr+"相对湿度:"+Match.Value
	Next 
	tmpstr=replace(tmpstr,"<div>&nbsp;相对湿度:","")
	tmpstr=replace(tmpstr,"</div>","")+"&nbsp;&nbsp;"
	
	RemoveDiv=tmpstr 
	Set objRegExp = Nothing 
	set Matches=nothing
End Function
'pattern = Pattern.compile("(?s)<div>&nbsp;温度:(.+?)</div>");
'matcher = pattern.matcher(svrRtnValue);
'if(matcher.find())
'responseValue+=" "+(matcher.group().replaceAll("<div>&nbsp;温度:","").replaceAll("</div>","").replaceAll("\n"," "));
'
'/*pattern = Pattern.compile("(?s)<div>&nbsp;体感温度:(.+?)</div>");
'matcher = pattern.matcher(svrRtnValue);
'if(matcher.find())
'responseValue+=",体感:"+(matcher.group().replaceAll("<div>&nbsp;体感温度:","").replaceAll("</div>","").replaceAll("\n"," "));*/
'
'pattern = Pattern.compile("(?s)<div>&nbsp;舒适度:(.+?)</div>");
'matcher = pattern.matcher(svrRtnValue);
'if(matcher.find())
'responseValue+=""+(matcher.group().replaceAll("<div>&nbsp;舒适度:","").replaceAll("</div>","").replaceAll("\n"," ").replaceAll(",",","));
'
'pattern = Pattern.compile("(?s)<div>&nbsp;风向:(.+?)</div>");
'matcher = pattern.matcher(svrRtnValue);
'if(matcher.find())
'responseValue+="$"+(matcher.group().replaceAll("<div>&nbsp;风向:","").replaceAll("</div>","").replaceAll("\n"," ").replaceAll("\t","").replaceAll(" ",""));
'
'pattern = Pattern.compile("(?s)<div>&nbsp;相对湿度:(.+?)</div>");
'matcher = pattern.matcher(svrRtnValue);
'if(matcher.find())
'responseValue+=",相对湿度"+(matcher.group().replaceAll("<div>&nbsp;相对湿度:","").replaceAll("</div>","").replaceAll("\n"," "));

caiurl="http://wap.weather.gov.cn/forecast/AHA/zhengzhou.html"
pcontent=BytesToBstr(GetBody(caiurl),"UTF-8") 
response.write(RemoveDiv(pcontent))
%>
本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号