This procedure is a simple crawl Google weather forecast code Oh, previously written are php,asp today I released a JSP get Goolge weather code OH.
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 (line). Equals ("") strcharutil.formatnullstr
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;
}