This program is a simple code for capturing Google weather forecasts. It used to write php. asp today I released a jsp to get goolge weather codes.
Public NodeList getWeatherDiv (String htmlUrl ){
NodeList res = null;
Try {
Parser parser = new Parser (htmlUrl );
Parser. setEncoding ("GBK ");
NodeFilter divFilter = new NodeClassFilter (Div. class );
OrFilter lastFilter = new OrFilter ();
LastFilter
. SetPredicates (new NodeFilter [] {divFilter });
NodeList nodeList = parser. parse (lastFilter );
Node [] nodes = nodeList. toNodeArray ();
For (int I = 0; I <nodes. length; I ++ ){
Node anode = (Node) nodes [I];
If (anode instanceof Div ){
Div mydiv = (Div) anode;
String className = mydiv. getAttribute ("class ");
If (className! = Null & className. equals ("e ")){
Res = mydiv. getChildren ();
}
}
}
} Catch (ParserException e ){
E. printStackTrace ();
}
Return res;
}
Public static void cleanCache (){
If (isStart) return;
IsStart = true;
TimerTask task = new TimerTask (){
Public void run (){
Iterator it = hmCache. entrySet (). iterator ();
While (it. hasNext ()){
Map. Entry entry = (Map. Entry) it. next ();
Object key = entry. getKey ();
String today = DateTimeUtil. format (new Date (), "yyyyMMdd ");
If (key. toString (). indexOf (today)> = 0 ){
It. remove ();
HmCache. remove (key );
}
}
}
};
Timer timer = new Timer ();
Timer. schedule (task, Calendar. getInstance (). getTime (), 24*3600*1000 );
}
Private void addWeatherDay (JSONObject json, int flag, String htmlContent ){
String tt = (flag = 0? "T" :( "t" + flag ));
Try {
Node anode = null;
Parser parser = Parser. createParser (htmlContent, "GBK ");
NodeFilter textFilter = new NodeClassFilter (TextNode. class );
NodeFilter imgFilter = new NodeClassFilter (ImageTag. class );
OrFilter lastFilter = new OrFilter ();
LastFilter. setPredicates (new NodeFilter [] {textFilter, imgFilter });
// String t = "", t_res = "", t_tp = "";
NodeList nodeList = parser. parse (lastFilter );
Node [] nodes = nodeList. toNodeArray ();
For (int I = 0; I <nodes. length; I ++ ){
Anode = (Node) nodes [I];
If (anode instanceof ImageTag ){
ImageTag img = (ImageTag) anode;
If (img! = Null ){
Json. put (tt + "_ res", img. getAttribute ("title "));
Json. put (tt + "_ result", img. getAttribute ("title "));
Json. put (tt + "_ tp", ("http://www.google.cn" + img. getImageURL ()));
}
} Else if (anode instanceof TextNode ){
TextNode text = (TextNode) anode;
String t = text. getText ();
If (t. indexOf ("°C")> 0 ){
Json. put (tt, t );
}
}
}
} Catch (Exception ex ){
Ex. printStackTrace ();
}
}
Private void getDivText (JSONObject json, String htmlContent ){
String line = "";
Node anode = null;
Div divnode = null;
Try {
Parser parser = Parser. createParser (htmlContent, "GBK ");
NodeFilter divFilter = new NodeClassFilter (Div. class );
OrFilter lastFilter = new OrFilter ();
LastFilter. setPredicates (new NodeFilter [] {divFilter });
NodeList nodeList = parser. parse (lastFilter );
Int idx = 0;
Node [] nodes = nodeList. toNodeArray ();
For (int I = 0; I <nodes. length; I ++ ){
Anode = (Node) nodes [I];
Line = "";
If (anode instanceof Div ){
Divnode = (Div) anode;
String className = StrCharUtil. formatNullStr (divnode. getAttribute ("class "));
String align = StrCharUtil. formatNullStr (divnode. getAttribute ("align "));
If (align. equals ("") continue;
If (className. equals ("") & align. equals ("center ")){
Line = divnode. getChildrenHTML ();
AddWeatherDay (json, idx, line );
Idx ++;
}
}
If (StrCharUtil. formatNullStr (line). equals (""))
Continue;
}
} Catch (ParserException pe ){
Pe. printStackTrace ();
}
}
Public JSONObject getWeather (String city ){
String today = DateTimeUtil. format (new Date (), "yyyyMMdd ");
If (hmCache. get (city + today )! = Null ){
Return hmCache. get (city + today );
}
JSONObject hm = new JSONObject ();
Hm. put ("zhishu ","");
Try {
City = getCityName (city );
Final String googleWeatherURL = "http://www.google.cn/search? Hl = zh-CN & newwindow = 1 & q = tq + "+ URLEncoder. encode (city," UTF-8 ") +" & aq = f & oq = ";
NodeList nodeListDiv = getWeatherDiv (googleWeatherURL );
Int idx = 0;
If (nodeListDiv! = Null ){
GetDivText (hm, nodeListDiv. toHtml ());
}
} Catch (Exception ex ){
Ex. printStackTrace ();
}
HmCache. put (city + today, hm );
Return hm;
}