Grabbed the National Bureau of Statistics in 2013 and inserted MySQL

Source: Internet
Author: User

Database creation Script:

/*navicat mysql data transfersource server          : localhostSource Server Version : 50621Source Host            : localhost:3306Source Database        : cmm-dbtarget server type    : mysqltarget  Server Version : 50621File Encoding          : 65001Date: 2015-05-25 19:05:58*/SET FOREIGN_KEY_CHECKS=0;-- ----------------- ------------- Table structure for  ' region '-- ----------------------------drop  table if exists  ' region '; create table  ' Region '   (   ' Area_code '  varchar ()  not null comment   ' Area code ',   ' Area_name '  varchar ( NOT NULL COMMENT  ' area)Name ',   ' Parent_code '  varchar ( NOT NULL COMMENT  ' Region parent code ',   ' place _order '  varchar ( NOT NULL DEFAULT  ' 0 '  COMMENT  ' display order ',   primary  KEY  (' Area_code ')  engine=innodb default charset=utf8 row_format=compact;--  ------------------------------ Records of region-- ----------------------------

Maven dependencies:

<dependency> <groupId>mysql</groupId> <artifactid>mysql-connector-java</artifactid > <version>5.1.18</version></dependency>

<dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <versio N>1.8.2</version></dependency>

Source:

import java.io.filewriter;import java.io.ioexception;import java.sql.connection;import  java.sql.drivermanager;import java.sql.preparedstatement;import java.sql.sqlexception;import  java.util.arraylist;import java.util.list;import java.util.regex.matcher;import  java.util.regex.pattern;import org.jsoup.jsoup;import org.jsoup.nodes.document;import  org.jsoup.nodes.element;import org.jsoup.select.elements;public class jsoupregion {/**   integer  */private static final String V_INTEGER =  "^-? [1-9]\\d*$]; Class region {public string getcode ()  {return code;} Public void setcode (String code)  {this.code = code;} Public string getname ()  {return name;} Public void setname (String name)  {this.name = name;} Public int getparentid ()  {return parenid;} Public voiD setparentid (Int parenid)  {this.parenid = parenid;} Public string gettype ()  {return type;} Public void settype (String type)  {this.type = type;} private string code;private string name;private string type;//  Township Type private  int parenid;} /** * @ description: url2document *  @param   @param  url *  @param   @return  *  @param   @throws  IOException *  @return  Document *  @throws  * /public static document url2doc (String url)  throws IOException {//  This method 403// return jsoup.connect (URL). get ();// return jsoup.connect (URL). Timeout (600 *  1000)// .useragent ("mozilla/5.0  (macintosh; intel mac os x 10_9_2)  AppleWebKit/537.36  (Khtml, like gecko)  chrome/33.0.1750.152 safari/537.36 ").Get ();//return jsoup.connect (URL). Timeout (600 * 1000). get (); /** *  validation is not an integer  *  *  @param  value *              string to validate   string to validate  *  @return   If it is a string that conforms to the format, return  <b >true </b>, otherwise for  <b>false </b> */public static boolean  Integer (String value)  {return match (v_integer, value);} /** *  @param  regex *              Regular expression strings  *  @param  str *              string to match  *  @return   if str  conforms to  regex's regular expression format, returns true,  otherwise returns  false;  */private static boolean match (STRING&NBSP;REGEX,&NBSP;STRING&NBSP;STR)  {Pattern  pattern = pattern.compile (regex); Matcher matcher = pattern.matcheR (str); return matcher.matches ();} /** *  Append files: Using filewriter *  *  @param  fileName *  @param   Content */public static void appendfile (string filename, string content)  {FileWriter writer = null;try {//  open a write file, the second parameter in the constructor true indicates that the file is written in append form writer  = new filewriter (filename, true); writer.write (content);}  catch  (ioexception e)  {e.printstacktrace ();}  finally {try {if  (Writer != null)  {writer.close ();}}  catch  (ioexception e)  {e.printstacktrace ();}} /** * @ description:  Get provinces  *  @param   @param  url *  @param   @return  *   @param   @throws  IOException *  @return  List<Region> *  @throws  */private static list<region> getprovince (String url)  throws  Ioexception {list<regioN> list = new arraylist<region> ();D ocument doc = url2doc (URL); Elements provicetr = doc.getelementsbyattributevalue ("Class",  "Provincetr");//  Get trfor  (ELEMENT&NBSP;E&NBSP;:&NBSP;PROVICETR)  {elements tds = e.select via CSS ("A[href") ");for  (Element element : tds)  {region region = new jsoupregion () . New region ();// region.setcode ("" "), Region.setcode (element.attr (" href "). substring (0, 2)); Region.setname (Element.text (). ReplaceAll ("<br />",  ")"); Region.settype (""); Region.setparentid ( 0); List.add (region);}} Return list;} /** * @ description:  Get the city  *  @param   @param  url *  @param   @return  *  @param   @throws  IOException *  @return  list<region> * @ Throws */private static list<region> getcity (String url) &NBSP;THROWS&NBSp;ioexception {list<region> list = new arraylist<region> ();D ocument  doc = url2doc (URL); Elements provicetr = doc.getelementsbyattributevalue ("Class",  "Citytr");//  Get trfor  (ELEMENT&NBSP;E&NBSP;:&NBSP;PROVICETR)  {elements tds = e.select via CSS ("A[href") ");for  (Element element : tds)  {if  (Integer (Element.text ()))  {continue;} Region region = new jsoupregion (). New region (); String code = element.attr ("href"). substring (3, 7); Region.setcode (code); Region.setname ( Element.text ()); Region.setparentid (Integer.valueof (code.substring (0, 2))); Region.settype (""); List.add ( region);}} Return list;} /** * @ description:  County  *  @param   @param  url *  @param   @return  *   @param   @throws  IOException *  @return  List<Region> *  @throws  */private&nBsp;static list<region> getcounty (String url)  throws IOException {List< Region> list = new arraylist<region> ();D ocument doc = url2doc (URL) ; Elements provicetr = doc.getelementsbyattributevalue ("Class",  "Countytr");//  Get trfor  (ELEMENT&NBSP;E&NBSP;:&NBSP;PROVICETR)  {elements tds = e.select via CSS ("A[href") ");for  (Element element : tds)  {if  (Integer (Element.text ()))  {continue;} Region region = new jsoupregion (). New region (); String code = element.attr ("href"). substring (3, 9); Region.setcode (code); Region.setname ( Element.text ()); Region.setparentid (Integer.valueof (code.substring (0, 4))); Region.settype (""); List.add ( region);}} Return list;} /** * @ description:  Town  *  @param   @param  url *  @param   @return  *   @param   @throws  ioexception *  @return  List<Region> *  @throws  */private static List< Region> gettown (String url)  throws IOException {List<Region> list  = new arraylist<region> ();D ocument doc = url2doc (URL); Elements provicetr = doc.getelementsbyattributevalue ("Class",  "Towntr");//  Get trfor  (ELEMENT&NBSP;E&NBSP;:&NBSP;PROVICETR)  {elements tds = e.select via CSS ("A[href") ");for  (Element element : tds)  {if  (Integer (Element.text ()))  {continue;} Region region = new jsoupregion (). New region (); String code = element.attr ("href"). substring (3, 12); Region.setcode (code); Region.setname ( Element.text ()); Region.setparentid (Integer.valueof (code.substring (0, 6))); Region.settype (""); List.add ( region);}} Return list;} /** * @ Description:  Village  *  @param   @param  url *  @param   @return  *  @param   @throws  IOException *  @return   list<region> *  @throws  */private static list<region> getvillage ( String url)  throws IOException {List<Region> list = new  Arraylist<region> ();D ocument doc = url2doc (URL); Elements provicetr = doc.getelementsbyattributevalue ("Class",  "Villagetr");//  Get trfor  (ELEMENT&NBSP;E&NBSP;:&NBSP;PROVICETR)  {elements trs = e.select ("tr") via CSS; for  (element element : trs)  {elements tds = element.select ("TD"); Region region = new jsoupregion (). New region ();for  (Element element2 :  tds)  {string value = element2.text ();if  (Integer (value)  &&  Value.length ()  == 3)  {region.settype (Element2.text ());} if  (inTeger (value)  && value.length ()  > 3)  {region.setcode (value); Region.setparentid (Integer.valueof (value.substring (0, 9));}  else {region.setname (value);}} List.add (region);}} Return list;} Public static void insertdb ()  {}/** *  @Description: *  @param  *   @return  Connection  return type  *  @throws  */private static Connection  Connection ()  throws ClassNotFoundException, SQLException {//  call Class.forName () method to load the driver Class.forName ("Com.mysql.jdbc.Driver"); string url =  "Jdbc:mysql://localhost:3306/cmm-db?useunicode=true&characterencoding=utf-8 &autoreconnect=true ";  // jdbc's Urlconnection conn = drivermanager.getconnection ( url,  "root",  "root"); return conn;} Public static void insertregion (region region)  throws classnotfoundexception,  sqlexception {cOnnection conn = connection (); Conn.setautocommit (false);//  the code that inserts the data string sql2 =   "Insert into region (Area_code,area_name,parent_code,place_order)  values (?,?,?,?)   "; Preparedstatement pst = conn.preparestatement (SQL2);p st.setstring (1, region.getcode ()); Pst.setstring (2, region.getname ());p st.setstring (3, region.getparentid () + "");p st.setstring (4,  Region.getcode ());p St.addbatch ();//  Perform bulk update pst.executebatch ();//  statement execution complete, commit this transaction conn.commit ();p st.close ( ); Conn.close ();} Public static void main (String[] args)  throws IOException,  classnotfoundexception, sqlexception {string url =  "Http://www.stats.gov.cn/tjsj/tjbz /tjyqhdmhcxhfdm/2013/"; List<region> all = new arraylist<region> (); List<region> province = getprovince (url +  "/index.html"); All.addAll (province); list<string> done&Nbsp;= new arraylist<string> ();//  is used to store for  (region regionprovince&nbsp) of the provinces that have been crawled;  province)  {//  traverse the province if  (Done.contains (Regionprovince.getcode ()))  {continue;} Insertregion (regionprovince); System.out.println (Regionprovince.getcode ()  + regionprovince.getname ()); List<region> city = getcity (Url + regionprovince.getcode ()  +  ". html") ;for  (region regioncity : city)  {//  Traverse City Insertregion (regioncity); System.out.println (Regioncity.getcode ()  +  "| |"  + regioncity.getname ()); List<region> county = getcounty (Url + regionprovince.getcode ()  +  "/"  + regioncity.getcode ()  +  ". html") All.addall (county);for  (region regioncounty  : county)  {//  Traverse County insertregion (Regioncounty); System.out.println (Regioncounty.getcode ()  +  "| |"  + regioncounty.getname ()); List<rEgion> town = gettown (Url + regionprovince.getcode ()  +  "/"  +  Regioncity.getcode (). substring (2, 4)  +  "/"  + regioncounty.getcode ()  +  ". HTML "), All.addall (town);for  (region regiontown : town)  {//  Traverse Zhen insertregion ( Regiontown); System.out.println (Regiontown.getcode ()  +  "| |"  + regiontown.getname ()); List<region> village = getvillage (Url + regionprovince.getcode ()  +  "/ " + regioncity.getcode (). substring (2, 4)  + "/" + regioncounty.getcode (). SUBSTRING (4, 6)  +  "/" + regiontown.getcode ()  +  ". html"); All.addall (village); for   (Region regionvillage : village)  {//  Traverse village insertregion (regionvillage); System.out.println (Regionvillage.getcode ()  +  "| |"  + regionvillage.getname ());}}}}}}


Grabbed the National Bureau of Statistics in 2013 and inserted MySQL

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.