Compact crawling (province, city (District Code \ zip code), county) data

Source: Internet
Author: User
Tags compact

The address information of the city needs to be used in recent projects, but few of the XML data downloaded from the Internet are the latest address information... the data is too old, leading to incomplete information in some regions. That's why I think the weather forecast official website has the latest and most comprehensive data. Paste the code, hoping to save some time for friends who are equally confused.

/*** @ Param var city name * @ returnstring array, 0 indicates zip code 1 indicates area code */@ suppresswarnings ("deprecation") Private string [] getzipcode (string var) {string [] code = new string [2]; string zipcode_s = "zip code:"; string zipcode_e = ""; string qhcode_s = "Area Code :"; string qhcode_e = "</TD>"; string encode = urlencoder. encode (VAR); try {URL url = new URL ("http://www.ip138.com/post/search.asp? Area = "+ encode +" & Action = area2zone "); bufferedreader BR = new bufferedreader (New inputstreamreader (URL. openstream (), "GBK"); For (string line; (line = BR. readline ())! = NULL;) {int zipnum = line. indexof (zipcode_s); If (zipnum> 1) {string STR = line. substring (zipnum + zipcode_s.length (); STR = Str. substring (0, str. indexof (zipcode_e); Code [0] = STR;} int qhnum = line. indexof (qhcode_s); If (qhnum> 1) {string STR = line. substring (qhnum + qhcode_s.length (); STR = Str. substring (0, str. indexof (qhcode_e); Code [1] = STR; break ;}} catch (exception e) {system. out. println (VAR + "\ T error" + E. tostring ();} return code;}/*** main program * @ throws exception */@ testpublic void main () throws exception {// 1: get all provinces treemap <string, string> provincesbuffer = getaddressinfo ("http://www.weather.com.cn//data/city3jdata/china.html"); element prcele = incluenthelper. createelement ("provinces"); // 2: Obtain the city element citysele = incluenthelper by province. createelement ("citys"); // 3: Obtain the district and county element distele = Documen Based on the province and city Thelper. createelement ("Districts"); int p = 1; int c = 1; int d = 1; for (Entry <string, string> PRC: provincesbuffer. entryset () {element province = incluenthelper. createelement ("Province"); province. addattribute ("ID", "" + (p )). addattribute ("provincename", PRC. getvalue ()). addtext (PRC. getvalue (); // get the zip code treemap <string, string> citybuffer = getaddressinfo ("http://www.weather.com.cn/data/city3jdata/provshi" + PRC. getkey () + ". html "); For (Entry <string, string> citys: citybuffer. entryset () {element city = incluenthelper. createelement ("city"); string [] zipcode = getzipcode (Citys. getvalue (); If (zipcode [0] = NULL | zipcode [1] = NULL) system. out. println ("missing" + Citys. getvalue () + "Zip or area code! "); City. addattribute ("ID", "" + C ). addattriys ("cityname", Citys. getvalue ()). addattribute ("PID", P + ""). addattribute ("zipcode", zipcode [0]). addattribute ("areacode", zipcode [1]). addtext (Citys. getvalue (); treemap <string, string> distsbuffer = getaddressinfo ("http://www.weather.com.cn/data/city3jdata/station/" + PRC. getkey () + "" + Citys. getkey () + ". html "); For (Entry <string, string> dists: distsbuffer. entryset () {String value = dists. getvalue (); If (value. equals (Citys. getvalue () continue; element district = incluenthelper. createelement ("District"); District. addattribute ("ID", "" + (d ++ )). addattribute ("districtname", dists. getvalue ()). addattribute ("CID", C + ""). addtext (dists. getvalue (); distele. add (district);} citysele. add (city); C ++;} prcele. add (province); P ++;} // 4: Save it to the local saveinf ("F: \ provinces. XML ", prcele); saveinf ("F: \ Citys. XML ", citysele); saveinf (" F: \ districts. XML ", distele);}/** save XML * @ Param savepath XML save path * @ Param varele root element */private void saveinf (string savepath, element varele) {document vardoc = incluenthelper. createdocument (); vardoc. add (varele); try {xmlwriter xmlwri = new xmlwriter (New fileoutputstream (new file (savepath), new outputformat ("\ t", true, "UTF-8 ")); xmlwri. write (vardoc); xmlwri. close ();} Catch (exception e) {system. out. println (savepath + "failed due to the following reasons"); throw new runtimeexception (e) ;}/ *** get information ** @ Param Address URL path * @ returnkey: information No. Value: Information name */private treemap <string, string> getaddressinfo (string address) {treemap <string, string> China = new treemap <string, string> (); bufferedreader BR = NULL; string buffer = NULL; try {URL url = new URL (Address); BR = new bufferedreader (New inputstrea Mreader (URL. openstream (), "UTF-8"); buffer = BR. readline ();} catch (exception e) {system. out. println ("error:" + E. getmessage ();} finally {If (BR! = NULL) Try {BR. close ();} catch (ioexception e) {e. printstacktrace () ;}} if (buffer = NULL) return China; buffer = buffer. replaceall ("\\{|\}| \" "," "); string [] splits = buffer. split (","); For (string SP: splits) {string [] split = sp. split (":"); If (split! = NULL & Split. length = 2) China. put (split [0], split [1]); elsesystem. out. println (Address);} buffer = NULL; return China ;}

Download XML data

Compact crawling (province, city (District Code \ zip code), county) data

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.