IP得到天气预报(2)———IP运算和数据处理
- 在上一Session中,我们把雅虎的数据地区吗和拼音提取到自己的数据库中去。那么现在就是要通过IP得到天气了。
- 首先,从网上找IP地址和城市的数据源。这个我是找到的了。一个Access数据库,当然这并不重要,什么类型的数据源都没关系。不过我还是因为个人的习惯转换成了SQL的。简单的看下表的结构吧。citycode是新提取的。而原有的数据库两张表格没动。IP1和IP2就是IP地址的范围。得到city,之后到city_Py中得到city和py,之后py就能通过cityname得到citycode了。而city用来显示的。

-
运算将真实IP转换为数字,比如北京的IP段是 : 1.1.0.0 -- 1.1.0.255
Startip: 1*256*256*256+1*256*256+0*256+0 = 16842752 (这个数字才是最终要放到数据库里)
Endip: 1*256*256*256+1*256*256+0*256+255 = 16843007 (这个数字才是最终要放到数据库里的)
最后就可以利用SQL语句查询数据库中的表来完成从ip地址到城市名的转换了。
在这里,代码中使用的函数是 CalculateIP(string ipAddress)具体实现很简单。不多啰嗦列举了。
列举下得到真实IP地址的算法吧。 -
protected static string GetReallIp()
{
string ip;
try
{
HttpRequest aRequest = HttpContext.Current.Request;
if (aRequest.ServerVariables["HTTP_VIA"] != null)
{
ip = aRequest.ServerVariables["HTTP_X_FORWARS_FOR"].ToString().Split(',')[0].Trim();
}
else
{
ip = aRequest.UserHostAddress;
}
}
catch (Exception e)
{
throw e;
}
return ip;
} -
那么既然有了IP了,就能通过数据库得到CHX城市的CODE了。因为雅虎的天气支持RSS了,可以通过拼接一个地址访问得到XML了。以北京为例。http://weather.yahooapis.com/forecastrss?p=CHXX0008&u=c 是RSS的链接地址。我们接下来要做的就是分析提取XML。
-
<yweather:location city="Beijing" region="" country="CH" />
<yweather:units temperature="C" distance="km" pressure="mb" speed="kph" />
<yweather:wind chill="0" direction="190" speed="11.27" />
<yweather:atmosphere humidity="26" visibility="9.99" pressure="0" rising="0" />
<yweather:astronomy sunrise="7:04 am" sunset="4:56 pm" />
- <item>
<title>Conditions for Beijing, CH at 4:00 pm CST</title>
<geo:lat>39.93</geo:lat>
<geo:long>116.28</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Beijing__CH/*http://weather.yahoo.com/forecast/CHXX0008_c.html</link>
<pubDate>Wed, 19 Nov 2008 4:00 pm CST</pubDate>
<yweather:condition text="Fair" code="34" temp="3" date="Wed, 19 Nov 2008 4:00 pm CST" /> -
这部分的代码分析就要靠XmlDocument来处理了。因为要很多数据信息,那么我采用datatable来存储所需要的内容。
-
protected DataSet GetWeather(string aUrlToXML)
{
XmlDocument aDoc = new XmlDocument();
aDoc.Load(aUrlToXML);
XmlNamespaceManager aManager = new XmlNamespaceManager(aDoc.NameTable);
aManager.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
//命名空间的处理过程
string addRSS = "http://xml.weather.yahoo.com/ns/rss/1.0";
XmlNodeList nodes = aDoc.GetElementsByTagName("forecast", addRSS);
DataSet dsWeather = new DataSet();
DataTable aTable = new DataTable("Weather");
dsWeather.Tables.Add(aTable);
dsWeather.Tables["Weather"].Columns.Add("Date", typeof(string));
dsWeather.Tables["Weather"].Columns.Add("Week", typeof(string));
//
.Tables的建立
if (nodes.Count > 0)
{
foreach (XmlNode node in nodes)
{
DataRow drowWeather = dsWeather.Tables["Weather"].NewRow();
//提取相应节点的数据内容。
drowWeather["Weather"] = node.SelectSingleNode("@text").Value;
drowWeather["Tlow"] = node.SelectSingleNode("@low").Value + "℃";
drowWeather["Thigh"] = node.SelectSingleNode("@high").Value + "℃";
dsWeather.Tables["Weather"].Rows.Add(drowWeather);
}
//其他一些命名空间的处理过程。
foreach (XmlNode node in nodesAtmosphere)
{
dsWeather.Tables["Weather"].Rows[0]["visibility"] = node.SelectSingleNode("@visibility").Value.ToString();
dsWeather.Tables["Weather"].Rows[0]["humidity"] = node.SelectSingleNode("@humidity").Value.ToString();
}
XmlNodeList nodesAstronomy = aDoc.GetElementsByTagName("astronomy", addRSS);
foreach (XmlNode node in nodesAstronomy)
{
dsWeather.Tables["Weather"].Rows[0]["sunrise"] = node.SelectSingleNode("@sunrise").Value.ToString();
dsWeather.Tables["Weather"].Rows[0]["sunset"] = node.SelectSingleNode("@sunset").Value.ToString();
}
return dsWeather;
}
} -
这个函数主要就是涉及的有GetElementsByTagName()不用说了,就是通过TagName得到一个List。循环访问这个List之后得到节点。 SelectSingleNode()函数就用来处理对应节点的内容了,也是提取Value来提取需要的数据了。把数据存储到Table中供我们来访问。
-
图片的显示。有一种方法是官方提供的数据。我们得到XML中的condition中的TEXT字段。就可以知道现在是晴朗还是多云,之后查找它的表格,得到图片的名字也好,数据也好。自己可以显示了。还有一种方法就是分析XML中的CDATA了。详细的信息下面再介绍吧。
- 简单加个链接目录,有兴趣的可以一起研究下:)
- IP得到天气预报(1)———数据分析存储
- IP得到天气预报(2)———IP运算和数据处理
- IP得到天气预报(3)———XML中CDATA的提取
- IP得到天气预报(4)———制作自己的RSS订阅
-
参考文章: http://www.cnblogs.com/zhangronghua/archive/2007/11/14/587497.html
-
参考文章:http://www.cnblogs.com/VisualStudio/archive/2008/10/14/1311187.html
作者:Alexliu(alex dotNet Learning)
出处:http://alexliu.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,转载请注明。并且保留文章链接。否则保留追究法律责任的权利。
浙公网安备 33010602011771号