刚赶工写了一个获取新浪天气预报的功能,顺便把代码分享了出来,以后再有这样的工作,大家可以直接把代码拿来使用。
1.利用新浪提供给的iframe直接嵌入,这种方式非常的简单,但是却没有交互性。代码如下:
<iframe frameborder="0" src="http://php.weather.sina.com.cn/widget/weather.php" scrolling="no" width="246" height="360"></iframe
2.抓取当天的天气,以指定格式输出。
涉及的核心代码如下:
Code
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("访问地址出错~~~ ");
}
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);
//得到风力
int windforceStartIndex = html.IndexOf("风力:", temperatureEndIndex);
int windforceEndIndex = html.IndexOf("<br>", windforceStartIndex);
string Windforce = html.Substring(windforceStartIndex + 3, windforceEndIndex - windforceStartIndex - 3);
if (Windforce.Contains("小于") && (!Windforce.Contains("等于"))) //判断风力是否含有"小于"或"小于等于"字样将,如果有的话,将其替换为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;
}
这里涉及到一个ConvertCode类,它的作用是用于把城市转换为对应的全国统一的编码,代码如下:
Code
using System;
using System.Collections.Generic;
using System.Web;
namespace WebApplication8
{
public class ConvertCode
{
public static string ConvertCodeByCity(string City)
{
string Code = "";
switch (City)
{
case "北京":
Code = "110100";
break;
case "天津":
Code = "120100";
break;
case "石家庄":
Code = "130101";
break;
case "唐山":
Code = "130201";
break;
case "秦皇岛":
Code = "130301";
break;
case "张家口":
Code = "130701";
break;
case "承德":
Code = "130801";
break;
case "廊坊":
Code = "131001";
break;
case "邯郸":
Code = "130401";
break;
case "邢台":
Code = "130501";
break;
case "保定":
Code = "130601";
break;
case "沧州":
Code = "130901";
break;
case "衡水":
Code = "133001";
break;
case "太原":
Code = "140101";
break;
case "大同":
Code = "140201";
break;
case "阳泉":
Code = "140301";
break;
case "晋城":
Code = "140501";
break;
case "朔州":
Code = "140601";
break;
case "忻州":
Code = "142201";
break;
case "离石":
Code = "142331";
break;
case "榆次":
Code = "142401";
break;
case "临汾":
Code = "142601";
break;
case "运城":
Code = "142701";
break;
case "长治":
Code = "140401";
break;
case "呼和浩特":
Code = "150101";
break;
case "包头":
Code = "150201";
break;
case "乌海":
Code = "150301";
break;
case "集宁":
Code = "152601";
break;
case "巴彦浩特": //这个城市搜索不到
Code = "152701";
break;
case "临河":
Code = "152801";
break;
case "鄂尔多斯": //这个城市搜索不到
Code = "152921";
break;
case "赤峰":
Code = "150401";
break;
case "通辽":
Code = "152301";
break;
case "锡林浩特":
Code = "152502";
break;
case "海拉尔": //这个城市搜索不到