前几天给大家分享了一下,怎么样通过jsoup来从国家统计局官网获取全国省市县镇村的数据。错过的朋友请点击这里
上文说到抓取到数据以后,我们怎么转换成我们想要格式呢?哈哈,解析方式可能很简单,但是有一点我是知道的,很多人是伸手党,那么我就把我的处理过程给大家分享出来,觉得不错的,请点个赞。

第一步:将获取到的txt文件转换成数据库文件:

这里需要备注一下,下文所有的资源压缩文件,解压密码都是我的博客园昵称。为什么要加密码给大家解释一下:前期发出的博文被其他很多站点爬取了,但是都没有原文链接或者转载说明,一点都不尊重原博文的版权。给大家带来的不便,敬请谅解。

上次博文处理后的文本数据下载地址:点击下载

废话不多说,直接上代码将抓取到的文本文件转换成数据库数据:

  1. 1 import java.io.BufferedReader;
  2. 2 import java.io.File;
  3. 3 import java.io.FileNotFoundException;
  4. 4 import java.io.FileReader;
  5. 5 import java.io.IOException;
  6. 6 import java.sql.Connection;
  7. 7 import java.sql.DriverManager;
  8. 8 import java.sql.SQLException;
  9. 9 import java.sql.Statement;
  10. 10
  11. 11 public class ResolveData1
  12. 12 {
  13. 13 private static Connection connection = null;
  14. 14
  15. 15 public static void main(String[] args)
  16. 16 {
  17. 17 initDB();
  18. 18
  19. 19 BufferedReader bufferedReader = null;
  20. 20 try
  21. 21 {
  22. 22 bufferedReader = new BufferedReader(new FileReader(new File("f:\\CityInfo.txt")));
  23. 23 String line = null;
  24. 24 while ((line = bufferedReader.readLine()) != null)
  25. 25 {
  26. 26 inser2DB(getCityName(line), getCityLevel(line), getCityCode(line));
  27. 27 System.out.println("处理中……");
  28. 28 }
  29. 29 } catch (FileNotFoundException e)
  30. 30 {
  31. 31 e.printStackTrace();
  32. 32 } catch (IOException e)
  33. 33 {
  34. 34 e.printStackTrace();
  35. 35 }
  36. 36 }
  37. 37
  38. 38 private static void initDB()
  39. 39 {
  40. 40 try
  41. 41 {
  42. 42 Class.forName("com.mysql.jdbc.Driver");
  43. 43 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/51houseservice", "数据库账户",
  44. 44 "数据库密码");
  45. 45 } catch (SQLException e)
  46. 46 {
  47. 47 e.printStackTrace();
  48. 48 } catch (ClassNotFoundException e)
  49. 49 {
  50. 50 e.printStackTrace();
  51. 51 }
  52. 52 }
  53. 53
  54. 54 private static String getCityName(String line)
  55. 55 {
  56. 56 return line.substring(0, line.indexOf("{"));
  57. 57 }
  58. 58
  59. 59 private static String getCityCode(String line)
  60. 60 {
  61. 61 return line.substring(line.indexOf("[") + 1, line.indexOf("]"));
  62. 62 }
  63. 63
  64. 64 private static int getCityLevel(String line)
  65. 65 {
  66. 66 return Integer.parseInt(line.substring(line.indexOf("{") + 1, line.indexOf("}")));
  67. 67 }
  68. 68
  69. 69 private static void inser2DB(String cityName, int cityLevel, String cityCode)
  70. 70 {
  71. 71 try
  72. 72 {
  73. 73
  74. 74 Statement createStatement = connection.createStatement();
  75. 75 createStatement
  76. 76 .executeUpdate("insert into _51houseservice_citys_copy(city_name_zh,city_level,city_code) values('"
  77. 77 + cityName + "'," + cityLevel + ",'" + cityCode + "')");
  78. 78 } catch (SQLException e)
  79. 79 {
  80. 80 e.printStackTrace();
  81. 81 }
  82. 82 }
  83. 83 }

执行完以上程序以后,那么数据就已经妥妥的放入数据库了。存入数据库的数据,相信各位码农都是高手,这些数据都成了你们砧板上的与鱼肉了吧。

第二步:将数据库的每一行数据添加上其父城市

细心的朋友一定发现了,上面的每一个城市数据都只是包含一自己本身的详细信息,但是省级城市与市级城市之间没有任何关联。基于树形结构的数据在数据库应该怎样存储我就不多说了。这里就直接贴上关联各上下级关联的城市的代码:

接下来的是处理过程中的代码:

  1. 1 package com.wyhousesevice.test;
  2. 2 import java.sql.Connection;
  3. 3 import java.sql.DriverManager;
  4. 4 import java.sql.ResultSet;
  5. 5 import java.sql.SQLException;
  6. 6 import java.sql.Statement;
  7. 7 import java.util.ArrayList;
  8. 8 import java.util.List;
  9. 9
  10. 10 public class ResolveData3
  11. 11 {
  12. 12 private static Connection connection;
  13. 13
  14. 14 public static void main(String[] args)
  15. 15 {
  16. 16 initDB();
  17. 17 try
  18. 18 {
  19. 19 // 获取源表中一行数据
  20. 20 ResultSet rs = getAllCitys();
  21. 21 rs.next();
  22. 22 while (rs.next())
  23. 23 {
  24. 24 // 如果该项存在父ID,则跳过设置
  25. 25 if (rs.getInt("parent_id") == 0)
  26. 26 {
  27. 27 List<String> parentCodes = getParentCodes(rs.getString("city_code"));
  28. 28 // 获取目标数据库的数据ID
  29. 29 int parentId = getParentId(parentCodes, rs.getInt("city_level") - 1);
  30. 30 doUpdate(rs.getInt("id"), parentId);
  31. 31 System.out.println("handling:" + rs.getInt("id"));
  32. 32 }
  33. 33 }
  34. 34 }
  35. 35 catch (SQLException e)
  36. 36 {
  37. 37 e.printStackTrace();
  38. 38 }
  39. 39
  40. 40 closeDB();
  41. 41 }
  42. 42
  43. 43 private static void doUpdate(int id, int parentId)
  44. 44 {
  45. 45 try
  46. 46 {
  47. 47 Statement statement = connection.createStatement();
  48. 48 statement.executeUpdate("UPDATE _51houseservice_citys_copy SET parent_id = " + parentId + " WHERE id = "
  49. 49 + id);
  50. 50 }
  51. 51 catch (SQLException e)
  52. 52 {
  53. 53 e.printStackTrace();
  54. 54 }
  55. 55 }
  56. 56
  57. 57 private static int getParentId(List<String> parentCodes, int level) throws SQLException
  58. 58 {
  59. 59 Statement statement = connection.createStatement();
  60. 60 for (String string : parentCodes)
  61. 61 {
  62. 62 ResultSet executeQuery = statement
  63. 63 .executeQuery("select * from _51houseservice_citys_copy where city_code='" + string
  64. 64 + "' and city_level=" + level);
  65. 65 if (executeQuery.next())
  66. 66 {
  67. 67 return executeQuery.getInt("id");
  68. 68 }
  69. 69 }
  70. 70 return -1;
  71. 71 }
  72. 72
  73. 73 private static List<String> getParentCodes(String cityCode)
  74. 74 {
  75. 75 List<String> dataList = new ArrayList<String>();
  76. 76
  77. 77 if (cityCode.endsWith("0"))
  78. 78 {
  79. 79 String code = rmvLastZero(cityCode);
  80. 80 for (int i = 1; i < code.length() - 1; i++)
  81. 81 {
  82. 82 String substring = code.substring(0, code.length() - i);
  83. 83 StringBuilder sb = new StringBuilder(substring);
  84. 84 for (int j = substring.length(); j < 12; j++)
  85. 85 {
  86. 86 sb.append("0");
  87. 87 }
  88. 88 dataList.add(sb.toString());
  89. 89 }
  90. 90 }
  91. 91 else
  92. 92 {
  93. 93 for (int i = 1; i < cityCode.length() - 1; i++)
  94. 94 {
  95. 95 String substring = cityCode.substring(0, cityCode.length() - i);
  96. 96 StringBuilder sb = new StringBuilder(substring);
  97. 97 for (int j = 1; j <= i; j++)
  98. 98 {
  99. 99 sb.append("0");
  100. 100 }
  101. 101 dataList.add(sb.toString());
  102. 102 }
  103. 103 }
  104. 104 return dataList;
  105. 105 }
  106. 106
  107. 107 private static String rmvLastZero(String cityCode)
  108. 108 {
  109. 109 while (cityCode.endsWith("0"))
  110. 110 {
  111. 111 cityCode = cityCode.substring(0, cityCode.length() - 1);
  112. 112 }
  113. 113 return cityCode;
  114. 114 }
  115. 115
  116. 116 private static ResultSet getAllCitys()
  117. 117 {
  118. 118 try
  119. 119 {
  120. 120 Statement createStatement = connection.createStatement();
  121. 121 return createStatement.executeQuery("select * from _51houseservice_citys_copy");
  122. 122 }
  123. 123 catch (SQLException e)
  124. 124 {
  125. 125 e.printStackTrace();
  126. 126 return null;
  127. 127 }
  128. 128 }
  129. 129
  130. 130 private static void closeDB()
  131. 131 {
  132. 132 if (connection != null)
  133. 133 {
  134. 134 try
  135. 135 {
  136. 136 connection.close();
  137. 137 }
  138. 138 catch (SQLException e)
  139. 139 {
  140. 140 e.printStackTrace();
  141. 141 }
  142. 142 }
  143. 143 }
  144. 144
  145. 145 private static void initDB()
  146. 146 {
  147. 147 try
  148. 148 {
  149. 149 Class.forName("com.mysql.jdbc.Driver");
  150. 150 connection = DriverManager
  151. 151 .getConnection("jdbc:mysql://localhost:3306/51houseservice", "数据库账户", "数据库密码");
  152. 152 }
  153. 153 catch (SQLException e)
  154. 154 {
  155. 155 e.printStackTrace();
  156. 156 }
  157. 157 catch (ClassNotFoundException e)
  158. 158 {
  159. 159 e.printStackTrace();
  160. 160 }
  161. 161 }
  162. 162 }

接下来就需要时间处理了,慢慢的处理.....最终得到的sql转储文件结果如下:点击下载

如果你觉得本博文对你有所帮助,请记得点击右下方的"推荐"哦,么么哒...

转载请注明出处:http://www.cnblogs.com/liushaofeng89/p/4937714.html

Jsoup获取全国地区数据(省市县镇村)(续) 纯干货分享的更多相关文章

  1. Jsoup获取全国地区数据(省市县镇村)

    最近手头在做一些东西,需要一个全国各地的地域数据,从省市区到县镇乡街道的.各种度娘,各种谷歌,都没找到一个完整的数据.最后功夫不负有心人,总算找到一份相对来说比较完整的数据,但是这里的数据也只是精确到 ...

  2. 省市县镇村五级地址智能提取(标准地址源来自国家统计局官网)SpringBoot+Elasticsearch 5.6

    项目目的 根据传入的地址,智能提取所属的省市县镇村5级地址.例如:用户输入“江苏南通嗨安李堡镇陈庄村8组88号”,我们需要提取到江苏省  南通市  海安县(即便用户输入了错字,“海”写成了“嗨”) 李 ...

  3. Jsoup获取部分页面数据失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.

    用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不符合要求. 请求代码如下: private static ...

  4. Jsoup获取部分页面数据失败 Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml

    用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不符合要求. 请求代码如下: private static ...

  5. 全中国的省市县镇乡村数据获取以及展示java源代码

    第一步.准备工作(数据源+工具): 数据源(截止目前最全面权威的官方数据):http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2013/ 爬取数据的工具 ...

  6. 获取上海地区AQI质量数据Python脚本

    一个获取上海地区AQI质量的Python脚本 https://github.com/yanyueoo7/Raspberrypi/blob/master/GetPmData_Shanghai.py #! ...

  7. 获取全国市以及地理坐标,各大坐标系北斗,百度,WGS-84坐标系的转换,有图,有代码

    1 先上坐标取到的值: 获取到的坐标部分如下: '北京市':[116.39564503788,39.92998577808], '天津市':[117.21081309155,39.1439299033 ...

  8. 全国天气预报数据API调用PHP示例

    本代码示例是基于PHP的聚合数据全国天气预报API服务请求的代码样例,使用前你需要: ①:通过https://www.juhe.cn/docs/api/id/39 申请一个天气预报API的appkey ...

  9. 全国地区sql表

    /** * 中国省市区--地区SQL表 */ CREATE TABLE `rc_district` ( `id` smallint(5) unsigned NOT NULL AUTO_INCREMEN ...

随机推荐

  1. u盘启动安装系统

    七彩虹主板如何设置U盘启动,本文就以七彩虹CG41主板为例详细的讲讲U盘启动设置方法. 几天前,想用U盘启动的时候,发现CG41主板启动顺序里找不到USB项,Boot Device Priority( ...

  2. 性能测试之Jmeter学习(四)

    本节主要讲解:如何创建Web测试计划 如何创建一个简单的测试计划,用于测试web站点? 1.明确测试需求:我们会模拟5个并发用户,对Jakarta Web站点的网个页面进行访问,另外每个并发用户都会运 ...

  3. 17.Identity Server 4回顾

    openIdConnect做完之后,在登陆之后这个RequireConsent.用户同意授权这一步没有做直接跳过了,可以理解为我们自己比较信任的这种客户端 就是这个应用是属于我们自己的,都是我们自己配 ...

  4. CodeForces 349B Color the Fence (DP)

    题意:给出1~9数字对应的费用以及一定的费用,让你输出所选的数字所能组合出的最大的数值. 析:DP,和01背包差不多的,dp[i] 表示费用最大为 i 时,最多多少位,然后再用两个数组,一个记录路径, ...

  5. FileWriter 写文件

    FileWriter fw = new FileWriter("C://Users//pc//Desktop//aaa.txt",true); fw.write("201 ...

  6. IntelliJ IDEA 激活

    方法1 进入ide主页面,help-register-license server,然后输入 http://idea.iteblog.com/key.php   或者   http://idea.la ...

  7. 【OpenJ_Bailian - 2795】金银岛(贪心)

    金银岛 Descriptions: 某天KID利用飞行器飞到了一个金银岛上,上面有许多珍贵的金属,KID虽然更喜欢各种宝石的艺术品,可是也不拒绝这样珍贵的金属.但是他只带着一个口袋,口袋至多只能装重量 ...

  8. performSegueWithIdentifier:sender里边的sender是啥意思

    performSegueWithIdentifier:sender里边的sender是啥意思啊?怎样用啊? [self performSegueWithIdentifier:@"pushSi ...

  9. Zju1610 Count the Colors(lazy标记详解)

    Description 画一些颜色段在一行上,一些较早的颜色就会被后来的颜色覆盖了. 你的任务就是要数出你随后能看到的不同颜色的段的数目.  Input 每组测试数据第一行只有一个整数n, 1 < ...

  10. day04 Calendar类