解析網(wǎng)頁(yè)中添加新浪天氣預(yù)報(bào)的幾種方法_.Net教程
教程Tag:暫無(wú)Tag,歡迎添加,賺取U幣!
推薦:使用ASP.NET內(nèi)置類生成圖片縮略圖及水印ASP.NETImageGeneration內(nèi)置了ImageResizeTransform類,可以實(shí)現(xiàn)圖片大小調(diào)整功能。也可以擴(kuò)展ImageTransform實(shí)現(xiàn)自己的圖片變換類。 下面使用ASP.NETImageGeneration生成圖片縮略圖及水印的代碼。 數(shù)據(jù)庫(kù): CREATETABLEt_images ( image_idINT, image_dataIM
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ò)~~~ ");
}
int count = html.Length;
int starIndex = html.IndexOf("<table ", 0, count);
int endIndex = html.IndexOf("</table>", starIndex, count - starIndex);
html = html.Substring(starIndex, endIndex - starIndex + 8);
//得到城市
int cityStartIndex = html.IndexOf("<b>", 0, html.Length);
int cityEndIndex = html.IndexOf("</b>", 0, html.Length);
string City = html.Substring(cityStartIndex + 3, cityEndIndex - cityStartIndex - 3);
//得到天氣
int weatherStartIndex = html.IndexOf("<b>", cityEndIndex);
int weatherEndIndex = html.IndexOf("</b>", weatherStartIndex);
string Weather = html.Substring(weatherStartIndex + 3, weatherEndIndex - weatherStartIndex - 3);
//得到溫度
int temperatureStartIndex = html.IndexOf("<b", weatherEndIndex);
int temperatureEndIndex = html.IndexOf("</b>", weatherEndIndex + 3);
string Temperature = html.Substring(temperatureStartIndex + 21, temperatureEndIndex - temperatureStartIndex - 21);
int int1 = Temperature.IndexOf("℃", 0);
int int2 = Temperature.IndexOf("~", 0);
int int3 = Temperature.IndexOf("℃", int2);
string MinTemperature = Temperature.Substring(int2 + 1, int3 - int2);
string MaxTemperature = Temperature.Substring(0, int2 - int1 + 2);
//得到風(fēng)力
int windforceStartIndex = html.IndexOf("風(fēng)力:", temperatureEndIndex);
int windforceEndIndex = html.IndexOf("<br>", windforceStartIndex);
string Windforce = html.Substring(windforceStartIndex + 3, windforceEndIndex - windforceStartIndex - 3);
if (Windforce.Contains("小于") && (!Windforce.Contains("等于"))) //判斷風(fēng)力是否含有"小于"或"小于等于"字樣將,如果有的話,將其替換為2-
{
//Windforce = Windforce.Replace("小于", "2-");
string strWindforce = Windforce.Substring(2, Windforce.Length - 3);
int minWindforce = Int32.Parse(strWindforce) - 1;
Windforce = Windforce.Replace("小于", minWindforce.ToString() + "-");
}
else if (Windforce.Contains("小于等于"))
{
string strWindforce = Windforce.Substring(4, Windforce.Length - 5);
int minWindforce = Int32.Parse(strWindforce) - 1;
Windforce = Windforce.Replace("小于等于", minWindforce.ToString() + "-");
}
ArrayList al = new ArrayList();
al.Add(City);
al.Add(Weather);
al.Add(MinTemperature);
al.Add(MaxTemperature);
al.Add(Windforce);
return al;
}
這里涉及到一個(gè)ConvertCode類,它的作用是用于把城市轉(zhuǎn)換為對(duì)應(yīng)的全國(guó)統(tǒng)一的編碼,
分享:揭秘.Net開(kāi)發(fā)常用十大輔助軟件1.EditPlus:文字處理軟件 EditPlus是一款功能強(qiáng)大的文字處理軟件。它可以充分的替換記事本,它也提供網(wǎng)頁(yè)作家及程序設(shè)計(jì)師許多強(qiáng)悍的功能。支持HTML、CSS、PHP、ASP、Perl、C/C++、Java、JavaScript、VBScript等多種語(yǔ)法的著色顯示。程序內(nèi)嵌網(wǎng)頁(yè)瀏覽器,其
相關(guān).Net教程:
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發(fā)送Email實(shí)例(可帶附件)
- js實(shí)現(xiàn)廣告漂浮效果的小例子
- asp.net Repeater 數(shù)據(jù)綁定的具體實(shí)現(xiàn)
- Asp.Net 無(wú)刷新文件上傳并顯示進(jìn)度條的實(shí)現(xiàn)方法及思路
- Asp.net獲取客戶端IP常見(jiàn)代碼存在的偽造IP問(wèn)題探討
- VS2010 水晶報(bào)表的使用方法
- ASP.NET中操作SQL數(shù)據(jù)庫(kù)(連接字符串的配置及獲取)
- asp.net頁(yè)面?zhèn)髦禍y(cè)試實(shí)例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲(chǔ)過(guò)程實(shí)現(xiàn)分頁(yè)示例代碼
- 相關(guān)鏈接:
- 教程說(shuō)明:
.Net教程- 解析網(wǎng)頁(yè)中添加新浪天氣預(yù)報(bào)的幾種方法
。