Java Crawl weather Forecast code

Source: Internet
Author: User
Tags json

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.