Java Crawl weather Forecast code
Use HTTP request to crawl the code of Google weather forecast and to cache the requested city weather forecast by day
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 (Web effects onobject web effects on,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;
}