访问www.163.com,首页的栏目里有当地的天气预报。可以猜想,这里的天气预报,应该是根据来访者的ip判断其所在地给出当地的天气情况。问了一些朋友,也证实了这一点。项目里也需要天气预报这个小栏目,同事做过一个(从其他站点抓取的),不过实现不了根据IP显示当地的天气情况,需要用户自行选择,而且抓取的站点属于小站….其可靠性值得怀疑。。所以就萌生了抓取网易的天气预报的想法。。。对页面进行分析。。发现显示天气预报的区域是一个IFrame,IFrame里嵌入了如下链接http://news.163.com/util/position1.html, 对这个地址访问直接跳转到另外一个链接http://news.163.com/weather/news/qx1/56294.html,此链接显示了天气情况,如图:
由此可以推测http://news.163.com/util/position1.html,是在根据来访者的IP判断所属区域,然后返回一个该地区所对应的区位码,如: 56294代表成都。如何让网易来帮我们的站点来访者判断所属区域,并给出天气情况,并显示在自己的站点页面上呢?还得继续分析。。因为http://news.163.com/util/position1.html,此链接一访问就转向到天气情况的链接,而无法查看源码。便猜想。。此页面肯定有些东西。。无奈之下。。WebRequest一下,出现了如下代码:
以下是引用片段:
1<script language="Javascript">
2var city = new Array("安徽","黑龙江","山东","北京","湖北","山西","福建","湖南","陕西","甘肃","吉林","上海","广东","江苏","四川","广西","江西","天津","贵州","辽宁","西藏","海南","内蒙古","新疆","河北","宁夏","云南","河南","青海","浙江","重庆");
3var weaths = new Array('58321','50953','54823','54511','57494','53772','59134','57679','57036','52889','54172','58367','59287','58238','56294','59431','58606','54527','57816','54342','55591','52856','53463','51463','53698','53614','56778','57083','52866','58457','57516');
4
5function getCookieVal (offset) {
6 var endstr = document.cookie.indexOf (";", offset);
7 if (endstr == -1)
8 endstr = document.cookie.length;
9 return unescape(document.cookie.substring(offset, endstr));
10}
11function GetCookie (name) {
12 var arg = name + "=";
13 var alen = arg.length;
14 var clen = document.cookie.length;
15 var i = 0;
16 while (i < clen) {
17 var j = i + alen;
18 if (document.cookie.substring(i, j) == arg)
19 return getCookieVal (j);
20 i = document.cookie.indexOf(" ", i) + 1;
21 if (i == 0)
22 break;
23 }
24 return "";
25}
26function SetCookie(cookieName,cookieValue,nDays) {
27 var today = new Date();
28 var expire = new Date();
29 if (nDays==null || nDays==0) nDays=1;
30 expire.setTime(today.getTime() + 3600000*24*nDays);
31 document.cookie = cookieName+"="+escape(cookieValue) + ";path=/;domain=.163.com;expires="+expire.toGMTString();
32}
33function getCityWeatherID(cityname){
34 for(i=0;i<city.length;i++){
35 if(city[i]==cityname){
36 return weaths[i];
37 }
38 }
39 return "54511";
40}
41
42var NTES_WeatherAddr = GetCookie("NTES_WeatherAddr");
43if (!NTES_WeatherAddr){
44 var loc = GetCookie("theaddr");
45 if(!loc){
46 document.write("<script type='text/javascript' src='http://202.108.39.152/ipquery'><" + "/script>");
47 }
48}
49</script>
50<script>
51if (!NTES_WeatherAddr){
52 NTES_WeatherAddr=getCityWeatherID(loc);
53}
54window.location.href="http://news.163.com/weather/news/qx1/"+NTES_WeatherAddr+".html";
55</script>
56
57