訪問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