ASP.NET获得新浪天气预报几种方式总结

 

1.利用新浪提供给的iframe直接嵌入,这种方式非常的简单,但是却没有交互性。代码如下:

<iframe frameborder="0" src="http://php.weather.sina.com.cn/widget/weather.php" 
scrolling="no" width="246" height="360"></iframe>

2.抓取当天的天气,以指定格式输出。

涉及的核心代码如下:



", thirdWindforceStartIndex);
string ThirdWindforce = Html.Substring(thirdWindforceStartIndex + 3, thirdWindforceEndIndex - thirdWindforceStartIndex - 3);

if (ThirdWindforce.Contains("至"))
{

 

}
else if (ThirdWindforce.Contains("<"))                  //判断风力是否含有"<"或"≤"字样将,如果有的话,将其替换为2-
{
string strWindforce = ThirdWindforce.Substring(1, ThirdWindforce.Length - 2);
int minWindforce = Int32.Parse(strWindforce) - 1;
ThirdWindforce = ThirdWindforce.Replace("<", minWindforce.ToString() + "-");
}
else if (ThirdWindforce.Contains("≤"))
{
string strWindforce = ThirdWindforce.Substring(1, ThirdWindforce.Length - 2);
int minWindforce = Int32.Parse(strWindforce) - 1;
ThirdWindforce = ThirdWindforce.Replace("≤", minWindforce.ToString() + "-");
}

 

#endregion

 


al.Add(FirstDayAndCity);
al.Add(FirstDate);
al.Add(FirstWeather);
al.Add(FirstMinTemperature);
al.Add(FirstMaxTemperature);
al.Add(FirstWindforce);

 

al.Add(secondDayAndCity);
al.Add(SecondDate);
al.Add(SecondWeather);
al.Add(SecondMinTemperature);
al.Add(SecondMaxTemperature);
al.Add(SecondWindforce);

 

al.Add(thirdDayAndCity);
al.Add(ThirdDate);
al.Add(ThirdWeather);
al.Add(ThirdMinTemperature);
al.Add(ThirdMaxTemperature);
al.Add(ThirdWindforce);
}
catch (Exception err)
{

 

}

 

return al;
}

3.获取三天以内的天气,以指定格式输出。

核心代码如下:

3.获取三天以内的天气,以指定格式输出。

核心代码如下:

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(" ", 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(" ", 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(" ", 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("

特殊说明,使用第三种方法获取天气预报,输入城市的时候可能会受新浪提供的服务的影响,可能有些城市搜不到,前两种方法应该是不会受影响的。另外,由于代码写的比较急,所以难免代码的质量就会有些问题,还请大家多多包涵。单纯从代码上看,可能确实没有什么难度,可是如果在您的工作中如果因为我的代码为您节省了一些时间,笔者深感欣慰了。

posted @ 2013-03-12 21:17  顶顶顶顶  阅读(490)  评论(0)    收藏  举报