怎么实现天气预报呀?
怎么实现天气预报呀?在开发一个大型网站时,一般都用到天气预报,用代码怎么实现呀 --------------------编程问答-------------------- 我也想知道
是不是网上有什么web service --------------------编程问答-------------------- webservice --------------------编程问答-------------------- <table width="170" height="62" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFF9EE">
<tr height="5">
<td colspan="3" bgcolor="#FFF9EE">
<iframe src="http://weather.265.com/weather.htm" width="168" height="54" frameborder="no"
marginwidth="0&quoat;" marginheight="0" scrolling="no"></iframe>
</td>
</tr>
</table> --------------------编程问答-------------------- <iframe id='ifm2' width='189' height='190' marginwidth='0' marginheight='0' hspace='0'
vspace='0' frameborder='0' scrolling='NO' src='http://weather.qq.com/inc/ss276.htm'>
</iframe>
直接这样不干脆一点 --------------------编程问答-------------------- 你到网上搜索一下“天气预报代码”,就可以找到你想要的样式的天气预报了。。 --------------------编程问答-------------------- 一般是用iframe做的吧, --------------------编程问答-------------------- 给你一个asp的页,你用其他的页掉它就行了
<%
Dim city
city=Request("city")
a = weather(""&city&"")'这里是城市名称
%>
document.write("<%=a(0)%>:<%=a(1)%> <%=a(2)%>~<%=a(3)%> <%=a(4)%>")
<%
on error resume next
function weather(city)
DIM Product(5)
content = getHTTPPage("http://tq.tom.com/china/index.html")
cc = split(content," align=center><td><font color=#005FC9>")
'response.write (cc(0))
for each a in cc
a1 = split(a,"</font></td><td>")'a1(0)为城市
if (a1(0)=city) then
'response.write a1(0)&a1(1)
a2 = split(a1(1),"</td><td>")'a2(0)天气,a2(1)最高温度,a2(2)最低温度
Product(0) = city
Product(1) = a2(0)
Product(2) = a2(1)
Product(3) = a2(2)
a3 = split(a2(3),"</td>")
Product(4) = a3(0)
exit for
end if
'response.write a&vbcrlf
next
weather = Product
end function
response.write city&vbcrlf&a2(0)&vbcrlf&a2(1)&vbcrlf&a2(2)&vbcrlf&wind
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 getHTTPPage(Path)
t = GetBody(Path)
getHTTPPage=BytesToBstr(t,"GB2312")
End function
Function GetBody(url)
on error resume next
Set Retrieval = CreateObject("Microsoft.XMLHTTP")
With Retrieval
.Open "Get", url, False, "", ""
.Send
GetBody = .ResponseBody
End With
Set Retrieval = Nothing
End Function
%> --------------------编程问答-------------------- 用这个页调它就行了
<HTML>
<HEAD></HEAD>
<body>
<SCRIPT src=weather.asp?city=北京>
document.write("<%=a(0)%>:<%=a(1)%> <%=a(2)%>~<%=a(3)%> <%=a(4)%>")
</SCRIPT>
</body>
</HTML> --------------------编程问答-------------------- <IFRAME ID='ifm2' WIDTH='189' HEIGHT='190' ALIGN='CENTER' MARGINWIDTH='0' MARGINHEIGHT='0' HSPACE='0' VSPACE='0' FRAMEBORDER='0' SCROLLING='NO' SRC='http://weather.qq.com/inc/ss296.htm'></IFRAME>
--------------------编程问答-------------------- 调用其他站点是最方便的办法..呵呵, --------------------编程问答-------------------- 调用其它网站是最简单和有效办法,不然你就得找到提供此服务的WebService
--------------------编程问答-------------------- 支持楼上观点! --------------------编程问答-------------------- up --------------------编程问答-------------------- up --------------------编程问答-------------------- public ArrayList GetWeather(string code)
{
/*
[0] "今日天气" string
[1] "北京" string
[2] "<img src=http://image2.sina.com.cn/dy/weather/images/figure/leizhenyu_big.gif width=45 height=45 alt=雷阵雨>" string
[3] "雷阵雨" string
[4] "29℃~21℃" string
[5] "2006年7月27日-28日" string
[6] " 风力:小于3级" string
[7] "空气质量:良 " string
[8] "紫外线强度:弱 " string
*/
string html = string.Empty;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://weather.sina.com.cn/iframe/weather/"+code+"_w.html");
request.Method = "Get";
//request.Timeout = 1;
request.ContentType = "application/x-www-form-urlencoded";
WebResponse response = request.GetResponse();
Stream s = response.GetResponseStream();
StreamReader sr = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));
html = sr.ReadToEnd();
s.Close();
sr.Close();
}
catch
{
throw new Exception("访问地址出错~~~");
}
int count = html.Length;
int starIndex = html.IndexOf("<table ",0,count-1);
int endIndex = html.IndexOf("</table>",starIndex,count-starIndex-1);
html = html.Substring(starIndex,endIndex-starIndex+8);
html = Regex.Replace(html,"<br>|</td>","|");
html = Regex.Replace(html,"<[^img][^>]*>","");
html = Regex.Replace(html,"\t|\r|\n","");
string [] strWeather = html.Split('|');
ArrayList array = new ArrayList();
foreach(string strTemp in strWeather)
{
if(strTemp != "")
array.Add(strTemp);
}
return array;
} --------------------编程问答-------------------- public ArrayList GetWeather(string code)
{
/*
[0] "今日天气" string
[1] "北京" string
[2] "<img src=http://image2.sina.com.cn/dy/weather/images/figure/leizhenyu_big.gif width=45 height=45 alt=雷阵雨>" string
[3] "雷阵雨" string
[4] "29℃~21℃" string
[5] "2006年7月27日-28日" string
[6] " 风力:小于3级" string
[7] "空气质量:良 " string
[8] "紫外线强度:弱 " string
*/
string html = string.Empty;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://weather.sina.com.cn/iframe/weather/"+code+"_w.html");
request.Method = "Get";
//request.Timeout = 1;
request.ContentType = "application/x-www-form-urlencoded";
WebResponse response = request.GetResponse();
Stream s = response.GetResponseStream();
StreamReader sr = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));
html = sr.ReadToEnd();
s.Close();
sr.Close();
}
catch
{
throw new Exception("访问地址出错~~~");
}
int count = html.Length;
int starIndex = html.IndexOf("<table ",0,count-1);
int endIndex = html.IndexOf("</table>",starIndex,count-starIndex-1);
html = html.Substring(starIndex,endIndex-starIndex+8);
html = Regex.Replace(html,"<br>|</td>","|");
html = Regex.Replace(html,"<[^img][^>]*>","");
html = Regex.Replace(html,"\t|\r|\n","");
string [] strWeather = html.Split('|');
ArrayList array = new ArrayList();
foreach(string strTemp in strWeather)
{
if(strTemp != "")
array.Add(strTemp);
}
return array;
}
+++++++++++++++++++++++++++++++
收藏中,谢谢了,哈。 --------------------编程问答-------------------- 汗~~~~~~~ --------------------编程问答-------------------- 最简单的方法是调用互联网上的WEB Service。 --------------------编程问答-------------------- //mark
xuexi``` --------------------编程问答-------------------- up --------------------编程问答-------------------- 同志们啦,我是新手,不懂怎么个调法呀? --------------------编程问答-------------------- http://weather.265.com/weather.htm --------------------编程问答-------------------- 用webclient读也可以 --------------------编程问答-------------------- 非常感谢glacier111(做网站,计算机毕业设计),你的方法最适合我,我做得啦,同时也非常感谢大伙的支持!真的非常感谢! --------------------编程问答-------------------- up --------------------编程问答-------------------- http://edzh.com/weather.shtml
实现代码(抓取中国气象局)
http://edzh.com/Html/Article/2006-12-8/Article_dbdd9ef8-6c39-4543-82a8-10cd497fd152.shtml
实现代码(抓取sina)
http://edzh.com/Html/Article/2006-12-6/Article_6f58d500-9a51-4280-b2a6-b939565fd4b4.shtml
--------------------编程问答-------------------- 我用的是yahoo的,感觉还不错 --------------------编程问答--------------------
mark --------------------编程问答-------------------- ding --------------------编程问答-------------------- ding --------------------编程问答-------------------- 要看楼主怎么实现了。无非是一个超链接,或是抓取。不过抓取有个缺点,就是如果网站改版了,你的程序就要修改了。 --------------------编程问答-------------------- 做了记号
` --------------------编程问答-------------------- 初学者用Web Service最方便,一来代码量少,二来可以入门Web Service,初步了解Web Service这个东东。 --------------------编程问答-------------------- --------------------编程问答-------------------- web service --------------------编程问答-------------------- 用webservice吧。这个:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx --------------------编程问答-------------------- <%@ WebService Language="c#" Class="WeatherWS.GetChinaWeather" %>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Net;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Services;
namespace WeatherWS
{
/// <summary>
/// GetChinaWeather 的摘要说明。
/// </summary>
[WebService(Namespace="http://flying.redv.com/monster")]
public class GetChinaWeather : System.Web.Services.WebService
{
public GetChinaWeather()
{
//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
InitializeComponent();
}
#region 组件设计器生成的代码
//Web 服务设计器所必需的
private IContainer components = null;
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
[WebMethod(Description="中国各城市(县)天气预报获取服务,可接受一字符串参数(可选的查询方式:·国内城市(县)全名·字首拼音缩写·电话区号·邮政编码,如查询徐州的天气情况可输入'徐州'或'xz'作为参数)")]
public DataSet getWeather(string strCity)
{
DataSet dsWeather = new DataSet();
try
{
const int maxDay=5;
string []time = new string[maxDay];//存储日期,从今天开始算起
string []weather = new string[maxDay];//保存天气情况数据
string []max = new string[maxDay];//保存最高温度数据
string []min = new string[maxDay];//保存最低温度数据
string []wind = new string[maxDay];//保存风向数据
//发送一个post请求到index.jsp页面以获取城市数据
Uri uri = new Uri("http://www.weathercn.com/forecastn/forcast/index.jsp?searchname="+System.Web.HttpUtility.UrlEncode(strCity,System.Text.Encoding.GetEncoding("GB2312")));
WebRequest wreq=WebRequest.Create(uri);
HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse();
string HTML ="";
Stream s=wresp.GetResponseStream();
StreamReader objReader = new StreamReader(s,System.Text.Encoding.Default);
HTML=objReader.ReadToEnd();
if(HTML==null||HTML=="")
return dsWeather;
HTML = HTML.ToLower();//全部转换为小写
if(HTML==null||HTML=="")
return dsWeather;
int head,tail,i;
//查找城市数据 如果没有找到 则返回一个空的dataset
head = HTML.IndexOf("查询结果:",0);
head = HTML.IndexOf("station_name=",head);
if(head==-1)
{
return dsWeather;
}
head = HTML.IndexOf("station_name=",head+1);
tail = HTML.IndexOf("'",head);
string strCityData = HTML.Substring(head,tail-head);//城市数据获取
string href = "http://www.weathercn.com/forecastn/forcast/forecastDetail.jsp?"+strCityData;
//根据城市数据去查询天气情况
wreq=WebRequest.Create(href);
wresp=(HttpWebResponse)wreq.GetResponse();
HTML ="";
s=wresp.GetResponseStream();
objReader = new StreamReader(s,System.Text.Encoding.Default);
HTML=objReader.ReadToEnd();
if(HTML==null||HTML=="")
return dsWeather;
HTML = HTML.ToLower();//全部转换为小写
DateTime dtNow = new DateTime();
dtNow = DateTime.Today;//获取系统当前日期
dtNow = dtNow.Subtract(TimeSpan.Parse("1"));
for(i=0;i<maxDay;i++)
{
dtNow = dtNow.Add(TimeSpan.Parse("1"));
time[i] = dtNow.ToShortDateString();//日期数据
}
//获取天气情况数据,总共maxDay天的数据
String date = DateTime.Now.Year.ToString()+"年"+DateTime.Now.Month.ToString()+"月";//当前年月
head = HTML.IndexOf(date,0);
head = HTML.IndexOf("<tr>",head);
for(i=0;i<maxDay;i++)
{
head = HTML.IndexOf("<td",head);
head = HTML.IndexOf("<img",head);
head = HTML.IndexOf("/",head);
head = HTML.IndexOf("/",head+1);
tail = HTML.IndexOf("_",head);
weather[i] = HTML.Substring(head+1,tail-head-1);
head = HTML.IndexOf("</td>",head);
}
//获取近maxDay天温度数据,包括最高温度和最低温度
for(i=0;i<maxDay;i++)
{
head = HTML.IndexOf("max",head);
head = HTML.IndexOf(">",head);
tail = HTML.IndexOf("<",head);
max[i] = HTML.Substring(head+1,tail-head-1);//最高温度
head = HTML.IndexOf("min",head);
head = HTML.IndexOf(">",head);
tail = HTML.IndexOf("<",head);
min[i] = HTML.Substring(head+1,tail-head-1);//最低温度
}
//最近maxDay天的风向数据
head = HTML.IndexOf("<tr",head);
for(i=0;i<maxDay;i++)
{
head = HTML.IndexOf("class",head);
head = HTML.IndexOf(">",head);
tail = HTML.IndexOf("<",head);
wind[i] = HTML.Substring(head+1,tail-head-1);//风向数据
}
//将数据填充到DataSet中去
DataTable dtWeather = new DataTable();
dtWeather.Columns.Add("日期");
dtWeather.Columns.Add("天气");
dtWeather.Columns.Add("最高温度");
dtWeather.Columns.Add("最低温度");
dtWeather.Columns.Add("风力风向");
for(i=0;i<maxDay;i++)
{
DataRow drWeather = dtWeather.NewRow();
drWeather["日期"] = time[i];
drWeather["天气"] = weather[i];
drWeather["最高温度"] = max[i];
drWeather["最低温度"] = min[i];
drWeather["风力风向"] = wind[i];
dtWeather.Rows.Add(drWeather);
}
dsWeather.Tables.Add(dtWeather);
dsWeather.AcceptChanges();
return dsWeather;
}
catch(Exception e)
{
//DO Something
return dsWeather;
}
}
}
}
--------------------编程问答-------------------- 到网上搜索一下
调用web service
听好用的
补充:.NET技术 , ASP.NET