溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

ASP.NET獲得新浪天氣預(yù)報(bào)幾種方式分別是什么

發(fā)布時(shí)間:2021-10-28 09:24:28 來(lái)源:億速云 閱讀:249 作者:柒染 欄目:編程語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)ASP.NET獲得新浪天氣預(yù)報(bào)幾種方式分別是什么,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

1.利用新浪提供給的iframe直接嵌入,這種方式非常的簡(jiǎn)單,但是卻沒(méi)有交互性。代碼如下:

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

2.抓取當(dāng)天的天氣,以指定格式輸出。

涉及的核心代碼如下:

public static ArrayList GetWeather(string code)
{
/*
[0] "北京 "string
[1] "雷陣雨 "string
[2] "9℃" string
[3] "29℃"string
[4] "小于3級(jí)"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("訪問(wèn)地址出錯(cuò)~~~ ");
}

 這里涉及到一個(gè)ConvertCode類(lèi),它的作用是用于把城市轉(zhuǎn)換為對(duì)應(yīng)的全國(guó)統(tǒng)一的編碼,代碼如下:

using System;
using System.Collections.Generic;
using System.Web;


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

3.獲取三天以?xún)?nèi)的天氣,以指定格式輸出。

核心代碼如下:

public static ArrayList GetThreeDayWeather(string City)
{
ArrayList al = new ArrayList();
/*
[0] "今天 北京"              string
[1] "2009-04-17,星期五"     string
[2] "晴轉(zhuǎn)多云"               string
[3] "12℃"                   string
[4] "25℃"                   string
[5] "2-3級(jí)"                  string
[6] "明天 北京"              string
[7] "2009-04-18,星期六"     string
[8] "陰轉(zhuǎn)陣雨"               string
[9] "11℃"                   string
[10] "21℃"                  string
[11] "2-3級(jí)"                 string
[12] "后天 北京"             string
[13] "2009-04-19,星期日"    string
[14] "多云轉(zhuǎn)陣雨"            string
[15] "9℃"                   string
[16] "20℃"                  string
[17] "2-3級(jí)"                 string         
*/
string Html = "";       //返回來(lái)的網(wǎng)頁(yè)的源碼
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("訪問(wèn)地址出錯(cuò)~~~ ");
}

//去除多余代碼,便于分析跟提高效率
int count = Html.Length;
int starIndex = Html.IndexOf("

", 0, count);
int endIndex = Html.IndexOf("

", 0);
Html = Html.Substring(starIndex, endIndex - starIndex);

try
{
#region 得到今天的天氣

//得到今天的標(biāo)識(shí)跟城市
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);

//得到今天的風(fēng)力
int firstWindforceStartIndex = Html.IndexOf("風(fēng)力:", firstTemperatureEndIndex);
int firstWindforceEndIndex = Html.IndexOf("

", firstWindforceStartIndex);
string FirstWindforce = Html.Substring(firstWindforceStartIndex + 3, firstWindforceEndIndex - firstWindforceStartIndex - 3);

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

}
else if (FirstWindforce.Contains("<"))                  //判斷風(fēng)力是否含有"<"或"≤"字樣將,如果有的話,將其替換為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 得到明天的天氣

//得到明天的標(biāo)識(shí)跟城市
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);

//得到明天的風(fēng)力
int secondWindforceStartIndex = Html.IndexOf("風(fēng)力:", secondTemperatureEndIndex);
int secondWindforceEndIndex = Html.IndexOf("

", secondWindforceStartIndex);
string SecondWindforce = Html.Substring(secondWindforceStartIndex + 3, secondWindforceEndIndex - secondWindforceStartIndex - 3);

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

}
else if (SecondWindforce.Contains("<"))                  //判斷風(fēng)力是否含有"<"或"≤"字樣將,如果有的話,將其替換為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 得到后天的天氣

//得到后天的標(biāo)識(shí)跟城市
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);

//得到后天的風(fēng)力
int thirdWindforceStartIndex = Html.IndexOf("風(fēng)力:", thirdTemperatureEndIndex);
int thirdWindforceEndIndex = Html.IndexOf("


特殊說(shuō)明,使用第三種方法獲取天氣預(yù)報(bào),輸入城市的時(shí)候可能會(huì)受新浪提供的服務(wù)的影響,可能有些城市搜不到,前兩種方法應(yīng)該是不會(huì)受影響的。另外,由于代碼寫(xiě)的比較急,所以難免代碼的質(zhì)量就會(huì)有些問(wèn)題,還請(qǐng)大家多多包涵。單純從代碼上看,可能確實(shí)沒(méi)有什么難度,可是如果在您的工作中如果因?yàn)槲业拇a為您節(jié)省了一些時(shí)間,筆者深感欣慰了。

另外,由于我的開(kāi)發(fā)環(huán)境是VS2008+sp1,如果您的vs版本較低,不妨把項(xiàng)目文件刪除掉,然后打開(kāi)您的VS,選擇打開(kāi)網(wǎng)站,然后定位到本程序的目錄,這樣就可以進(jìn)行代碼的編譯跟蹤調(diào)試了。

再補(bǔ)充一點(diǎn),有人可能對(duì)代碼中出現(xiàn)的:System.Environment.NewLine 這句代碼比較迷糊,因?yàn)檫@個(gè)是.NET提供的簡(jiǎn)單的換行方法,我推薦大家以后能夠使用這種.net提供給大家的高效、簡(jiǎn)便的方法。最關(guān)鍵的是它不容易出錯(cuò)。

關(guān)于ASP.NET獲得新浪天氣預(yù)報(bào)幾種方式分別是什么就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI