ASP.NET获得新浪天气预报几种方式分别是什么
这篇文章将为大家详细讲解有关ASP.NET获得新浪天气预报几种方式分别是什么,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
创新互联是一家专注于成都网站建设、做网站与策划设计,崇仁网站建设哪家好?创新互联做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:崇仁等地区。崇仁做网站价格咨询:028-86922220
1.利用新浪提供给的iframe直接嵌入,这种方式非常的简单,但是却没有交互性。代码如下:
2.抓取当天的天气,以指定格式输出。
涉及的核心代码如下:
public static ArrayList GetWeather(string code) { /* [0] "北京 "string [1] "雷阵雨 "string [2] "9℃" string [3] "29℃"string [4] "小于3级"string */ string html = ""; 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 (Exception err) { throw new Exception("访问地址出错~~~ "); }
这里涉及到一个ConvertCode类,它的作用是用于把城市转换为对应的全国统一的编码,代码如下:
using System; using System.Collections.Generic; using System.Web; ", thirdWindforceStartIndex); string ThirdWindforce = Html.Substring(thirdWindforceStartIndex + 3, thirdWindforceEndIndex - thirdWindforceStartIndex - 3);
3.获取三天以内的天气,以指定格式输出。
核心代码如下:
public static ArrayList GetThreeDayWeather(string City) { ArrayList al = new ArrayList(); /* [0] "今天 北京" string [1] "2009-04-17,星期五" string [2] "晴转多云" string [3] "12℃" string [4] "25℃" string [5] "2-3级" string [6] "明天 北京" string [7] "2009-04-18,星期六" string [8] "阴转阵雨" string [9] "11℃" string [10] "21℃" string [11] "2-3级" string [12] "后天 北京" string [13] "2009-04-19,星期日" string [14] "多云转阵雨" string [15] "9℃" string [16] "20℃" string [17] "2-3级" string */ string Html = ""; //返回来的网页的源码 ASCIIEncoding encoding = new ASCIIEncoding(); string postData = string.Format("city=" + City); byte[] data = encoding.GetBytes(postData);
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://php.weather.sina.com.cn/search.php?city=" + System.Web.HttpContext.Current.Server.UrlEncode(City) + "&f=1&dpc=1");
request.Method = "Get";
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 (Exception err)
{
throw new Exception("访问地址出错~~~ ");
}
//去除多余代码,便于分析跟提高效率
int count = Html.Length;
int starIndex = Html.IndexOf("
", 0, count);
int endIndex = Html.IndexOf("
", 0);
Html = Html.Substring(starIndex, endIndex - starIndex);
try
{
#region 得到今天的天气
//得到今天的标识跟城市
int firstDayAndCityStartIndex = Html.IndexOf("
", 0);
int firstDayAndCityEndIndex = Html.IndexOf("
", 0);
string FirstDayAndCity = Html.Substring(firstDayAndCityStartIndex + 4, firstDayAndCityEndIndex - firstDayAndCityStartIndex - 4);
//得到今天的日期跟星期
int firstDateStartIndex = Html.IndexOf("
", firstDayAndCityEndIndex);
int firstDateEndIndex = Html.IndexOf("
", firstDayAndCityEndIndex);
string FirstDate = Html.Substring(firstDateStartIndex + 3, firstDateEndIndex - firstDateStartIndex - 3).Replace(" ", ",");
//得到今天的天气
int firstWeatherStartIndex = Html.IndexOf("
", firstDateEndIndex);
int firstWeatherEndIndex = Html.IndexOf(" ", firstWeatherStartIndex + 24);
string FirstWeather = Html.Substring(firstWeatherStartIndex + 24, firstWeatherEndIndex - firstWeatherStartIndex - 24);
//得到今天的温度
int firstTemperatureStartIndex = firstWeatherEndIndex + 1;
int firstTemperatureEndIndex = Html.IndexOf("
", firstTemperatureStartIndex);
string FirstTemperature = Html.Substring(firstTemperatureStartIndex, firstTemperatureEndIndex - firstTemperatureStartIndex);
int int1 = FirstTemperature.IndexOf("℃", 0);
int int2 = FirstTemperature.IndexOf("~", 0);
int int3 = FirstTemperature.IndexOf("℃", int2);
string FirstMinTemperature = FirstTemperature.Substring(int2 + 1, int3 - int2);
string FirstMaxTemperature = FirstTemperature.Substring(0, int2 - int1 + 2);
//得到今天的风力
int firstWindforceStartIndex = Html.IndexOf("风力:", firstTemperatureEndIndex);
int firstWindforceEndIndex = Html.IndexOf("
", firstWindforceStartIndex);
string FirstWindforce = Html.Substring(firstWindforceStartIndex + 3, firstWindforceEndIndex - firstWindforceStartIndex - 3);
if (FirstWindforce.Contains("至"))
{
}
else if (FirstWindforce.Contains("<")) //判断风力是否含有"<"或"≤"字样将,如果有的话,将其替换为2-
{
string strWindforce = FirstWindforce.Substring(1, FirstWindforce.Length - 2);
int minWindforce = Int32.Parse(strWindforce) - 1;
FirstWindforce = FirstWindforce.Replace("<", minWindforce.ToString() + "-");
}
else if (FirstWindforce.Contains("≤"))
{
string strWindforce = FirstWindforce.Substring(1, FirstWindforce.Length - 2);
int minWindforce = Int32.Parse(strWindforce) - 1;
FirstWindforce = FirstWindforce.Replace("≤", minWindforce.ToString() + "-");
}
#endregion
#region 得到明天的天气
//得到明天的标识跟城市
int secondDayAndCityStartIndex = Html.IndexOf("
", firstWindforceEndIndex);
int secondDayAndCityEndIndex = Html.IndexOf("
", secondDayAndCityStartIndex);
string secondDayAndCity = Html.Substring(secondDayAndCityStartIndex + 4, secondDayAndCityEndIndex - secondDayAndCityStartIndex - 4);
//得到明天的日期跟星期
int secondDateStartIndex = Html.IndexOf("
", secondDayAndCityEndIndex);
int secondDateEndIndex = Html.IndexOf("
", secondDateStartIndex);
string SecondDate = Html.Substring(secondDateStartIndex + 3, secondDateEndIndex - secondDateStartIndex - 3).Replace(" ", ",");
//得到明天的天气
int secondWeatherStartIndex = Html.IndexOf("
", secondDateEndIndex);
int secondWeatherEndIndex = Html.IndexOf(" ", secondWeatherStartIndex + 24);
string SecondWeather = Html.Substring(secondWeatherStartIndex + 24, secondWeatherEndIndex - secondWeatherStartIndex - 24);
//得到明天的温度
int secondTemperatureStartIndex = secondWeatherEndIndex + 1;
int secondTemperatureEndIndex = Html.IndexOf("
", secondTemperatureStartIndex);
string SecondTemperature = Html.Substring(secondTemperatureStartIndex, secondTemperatureEndIndex - secondTemperatureStartIndex);
int int4 = SecondTemperature.IndexOf("℃", 0);
int int5 = SecondTemperature.IndexOf("~", 0);
int int6 = SecondTemperature.IndexOf("℃", int2);
string SecondMinTemperature = SecondTemperature.Substring(int5 + 1, int6 - int5);
string SecondMaxTemperature = SecondTemperature.Substring(0, int5 - int4 + 2);
//得到明天的风力
int secondWindforceStartIndex = Html.IndexOf("风力:", secondTemperatureEndIndex);
int secondWindforceEndIndex = Html.IndexOf("
", secondWindforceStartIndex);
string SecondWindforce = Html.Substring(secondWindforceStartIndex + 3, secondWindforceEndIndex - secondWindforceStartIndex - 3);
if (SecondWindforce.Contains("至"))
{
}
else if (SecondWindforce.Contains("<")) //判断风力是否含有"<"或"≤"字样将,如果有的话,将其替换为2-
{
string strWindforce = SecondWindforce.Substring(1, FirstWindforce.Length - 2);
int minWindforce = Int32.Parse(strWindforce) - 1;
SecondWindforce = SecondWindforce.Replace("<", minWindforce.ToString() + "-");
}
else if (SecondWindforce.Contains("≤"))
{
string strWindforce = SecondWindforce.Substring(1, SecondWindforce.Length - 2);
int minWindforce = Int32.Parse(strWindforce) - 1;
SecondWindforce = SecondWindforce.Replace("≤", minWindforce.ToString() + "-");
}
#endregion
#region 得到后天的天气
//得到后天的标识跟城市
int thirdDayAndCityStartIndex = Html.IndexOf("
", secondWindforceEndIndex);
int thirdDayAndCityEndIndex = Html.IndexOf("
", thirdDayAndCityStartIndex);
string thirdDayAndCity = Html.Substring(thirdDayAndCityStartIndex + 4, thirdDayAndCityEndIndex - thirdDayAndCityStartIndex - 4);
//得到后天的日期跟星期
int thirdDateStartIndex = Html.IndexOf("
", thirdDayAndCityEndIndex);
int thirdDateEndIndex = Html.IndexOf("
", thirdDateStartIndex);
string ThirdDate = Html.Substring(thirdDateStartIndex + 3, thirdDateEndIndex - thirdDateStartIndex - 3).Replace(" ", ",");
//得到后天的天气
int thirdWeatherStartIndex = Html.IndexOf("
", thirdDateEndIndex);
int thirdWeatherEndIndex = Html.IndexOf(" ", thirdWeatherStartIndex + 24);
string ThirdWeather = Html.Substring(thirdWeatherStartIndex + 24, thirdWeatherEndIndex - thirdWeatherStartIndex - 24);
//得到后天的温度
int thirdTemperatureStartIndex = thirdWeatherEndIndex + 1;
int thirdTemperatureEndIndex = Html.IndexOf("
", thirdTemperatureStartIndex);
string ThirdTemperature = Html.Substring(thirdTemperatureStartIndex, thirdTemperatureEndIndex - thirdTemperatureStartIndex);
int int7 = ThirdTemperature.IndexOf("℃", 0);
int int8 = ThirdTemperature.IndexOf("~", 0);
int int9 = ThirdTemperature.IndexOf("℃", int2);
string ThirdMinTemperature = ThirdTemperature.Substring(int8 + 1, int9 - int8);
string ThirdMaxTemperature = ThirdTemperature.Substring(0, int8 - int7 + 2);
//得到后天的风力
int thirdWindforceStartIndex = Html.IndexOf("风力:", thirdTemperatureEndIndex);
int thirdWindforceEndIndex = Html.IndexOf("
特殊说明,使用第三种方法获取天气预报,输入城市的时候可能会受新浪提供的服务的影响,可能有些城市搜不到,前两种方法应该是不会受影响的。另外,由于代码写的比较急,所以难免代码的质量就会有些问题,还请大家多多包涵。单纯从代码上看,可能确实没有什么难度,可是如果在您的工作中如果因为我的代码为您节省了一些时间,笔者深感欣慰了。
另外,由于我的开发环境是VS2008+sp1,如果您的vs版本较低,不妨把项目文件删除掉,然后打开您的VS,选择打开网站,然后定位到本程序的目录,这样就可以进行代码的编译跟踪调试了。
再补充一点,有人可能对代码中出现的:System.Environment.NewLine 这句代码比较迷糊,因为这个是.NET提供的简单的换行方法,我推荐大家以后能够使用这种.net提供给大家的高效、简便的方法。最关键的是它不容易出错。
关于ASP.NET获得新浪天气预报几种方式分别是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
新闻标题:ASP.NET获得新浪天气预报几种方式分别是什么
文章路径:http://ybzwz.com/article/ppsjsh.html