对应本博客还有一个简化版的,请参看:省市区级联SQL文件)

说明

费了好大的劲把数据从官网上爬下来并导入到MySQL中

国家统计局官网地址:http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2020/54/5402.html

爬虫代码

package com.hc;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hc.domain.*;
import com.hc.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;/*** 全国省市县镇村数据爬取** @author 梁云亮*/
@Slf4j
@SpringBootTest
public class InitAdd5Tables {/*** 建立连接*/private Document connect(String url) {if (url == null || url.isEmpty()) {throw new IllegalArgumentException("无效的url");}try {return Jsoup.connect(url).timeout(100 * 1000).get();} catch (IOException e) {System.out.println(url+"地址不存在");return null;}}/*** 获取所有的省份** @return*/public List<String> getProvinces() {List<String> res = new ArrayList<>();Document connect = connect("http://localhost:8080/2020/default.htm");Elements rowProvince = connect.select("tr.provincetr");for (Element provinceElement : rowProvince) {// 遍历每一行的省份城市Elements select = provinceElement.select("a");for (Element province : select) {// 每一个省份(四川省)String name = province.text();String code = province.select("a").attr("href");res.add(code.substring(0, code.lastIndexOf(".")) + "*" + name);}}return res;}@Testpublic void testGetProvince() {getProvinces().forEach(System.out::println);}@Resourceprivate ProvinceMapper provinceMapper;@Testvoid insertProvinces() {List<Province> list = new ArrayList<>();for (String p : getProvinces()) {String[] split = p.split("\\*");Province province = Province.builder().code(split[0]).name(split[1]).build();list.add(province);}//list.forEach(System.out::println);int res = provinceMapper.batchInsert(list);System.out.println(res);}/*** 根据省份编号获取该省份下所有的市** @param provinceCode 省份编号* @return*/public List<String> getCitiesByProvince(String provinceCode) {List<String> res = new ArrayList<>();Document connect = connect("http://localhost:8080/2020/" + provinceCode + ".html");Elements rowCity = connect.select("tr.citytr");for (Element cityElement : rowCity) {// 遍历每一行的省份城市String name = cityElement.select("td").text();String[] split = name.split(" ");res.add(split[0].substring(0, 4) + "*" + split[1]);}return res;}@Testpublic void testGetCitiesByProvince() {getCitiesByProvince("41").forEach(System.out::println);}@Resourceprivate CityMapper cityMapper;@Testvoid insertCities() {List<String> pList = getProvinces();for (String p : pList) {List<City> list = new ArrayList<>();String[] split = p.split("\\*");List<String> cList = getCitiesByProvince(split[0]);Province pp = provinceMapper.selectOne(new QueryWrapper<Province>().eq("code", split[0]));for (String c : cList) {String[] tmp = c.split("\\*");City city = City.builder().name(tmp[1]).code(tmp[0]).provinceId(pp.getId()).build();//System.out.println(city);list.add(city);}//一个省一个省的添加int res = cityMapper.batchInsert(list);System.out.println(res);}}/*** 根据省市编号获取该省份下所有的县** @param cityCode 市编号* @return*/public List<String> getCountriesByCity(String cityCode) {List<String> res = new ArrayList<>();Document connect = connect("http://localhost:8080/2020/" + cityCode + ".html");Elements rowCountry = connect.select("tr.countytr");if (rowCountry.size() == 0) {Elements townCountry = connect.select("tr.towntr");for (Element townElement : townCountry) {String txt = townElement.select("td").text();String[] split = txt.split(" ");res.add(split[0].substring(0, 9) + "*" + split[1]);//比如海南省下的儋州市,只有4级目录,没有country}} else {for (Element countryElement : rowCountry) {// 遍历每一行的省份城市String txt = countryElement.select("td").text();String[] split = txt.split(" ");res.add(split[0].substring(0, 6) + "*" + split[1]);}}return res;}@Testvoid testGetCountiesByProvince() {getCountriesByCity("46/4604").forEach(System.out::println);}@Resourceprivate CountryMapper countryMapper;@Testvoid insertCountry() {List<String> pList = getProvinces();for (int i = 0; i < pList.size(); i++) {String p = pList.get(i);String[] split = p.split("\\*");//System.out.println(split[0] +"  "+split[1]); // 13  河北省List<String> cList = getCitiesByProvince(split[0]);for (String c : cList) {String[] split2 = c.split("\\*");//System.out.println(split2[0] +" * "+split2[1]);//1301 * 石家庄市List<String> c2List = getCountriesByCity(split2[0].substring(0, 2) + "/" + split2[0]);City city = cityMapper.selectOne(new QueryWrapper<City>().eq("code", split2[0]));List<Country> list = new ArrayList<>();for (String c2 : c2List) {String[] split3 = c2.split("\\*");System.out.println(split3[0] + " * " + split3[1]);Country country = Country.builder().name(split3[1]).code(split3[0]).cityId(city.getId()).build();list.add(country);}int res = countryMapper.batchInsert(list);System.out.println(res);}}}/*** 根据县编号获取乡** @param countryCode* @return*/public List<String> getTownsByCountry(String countryCode) {List<String> res = new ArrayList<>();Document connect = connect("http://localhost:8080/2020/" + countryCode + ".html");if (connect != null) {Elements rowTown = connect.select("tr.towntr");if (rowTown.size() == 0) {Elements rowVillage = connect.select("tr.villagetr");for (Element villageElement : rowVillage) {String txt = villageElement.select("td").text();String[] split = txt.split(" ");res.add(split[1] + "*" + split[2]);//比如海南省下的儋州市,只有4级目录,没有country}} else {for (Element townElement : rowTown) {// 遍历每一行的省份城市String txt = townElement.select("td").text();String[] split = txt.split(" ");res.add(split[0].substring(0, 9) + "*" + split[1]);}}}return res;}@Testvoid testGetTownsByCountry() {getTownsByCountry("41/01/410122").forEach(System.out::println);}@Resourceprivate TownMapper townMapper;@Testvoid insertTown() {List<String> pList = getProvinces();for (int i = 0; i < pList.size(); i++) {String p = pList.get(i);String[] split = p.split("\\*");//System.out.println(split[0] +"  "+split[1]); // 13  河北省List<String> cList = getCitiesByProvince(split[0]);for (String c : cList) {String[] split2 = c.split("\\*");//System.out.println(split2[0] +" * "+split2[1]);//1301 * 石家庄市List<String> c2List = getCountriesByCity(split2[0].substring(0, 2) + "/" + split2[0]);for (String c2 : c2List) {String[] split3 = c2.split("\\*");//System.out.println(split3[0] + " * " + split3[1]);  //130324 * 卢龙县List<String> tList = getTownsByCountry(split3[0].substring(0, 2) + "/" + split3[0].substring(2, 4) + "/" + split3[0]);List<Town> list = new ArrayList<>();Country country = countryMapper.selectOne(new QueryWrapper<Country>().eq("code", split3[0]));for (String t : tList) {String[] split4 = t.split("\\*");//System.out.println(split4[0] + " * " + split4[1]);Town town = Town.builder().name(split4[1]).code(split4[0]).countryId(country.getId()).build();//System.out.println(town);list.add(town);}if (list.size() != 0) {System.out.println(list);int res = townMapper.batchInsert(list);System.out.println(res);}}}}}/*** 根据乡编号获取村** @param townCode* @return*/public List<String> getVillagesByCountry(String townCode) {List<String> res = new ArrayList<>();Document connect = connect("http://localhost:8080/2020/" + townCode + ".html");Elements rowVillage = connect.select("tr.villagetr");for (Element villageElement : rowVillage) {// 遍历每一行的省份城市String txt = villageElement.select("td").text();String t = txt.substring(13);res.add(t);}return res;}@Testvoid testGetVillagesByCountry() {getVillagesByCountry("41/01/22/410122104").forEach(System.out::println);}@Resourceprivate VillageMapper villageMapper;@Testvoid insertVillage() {List<String> pList = getProvinces();for (int i = 25; i < pList.size(); i++) {String p = pList.get(i);String[] split = p.split("\\*");//System.out.println(split[0] +"  "+split[1]); // 13  河北省List<String> cList = getCitiesByProvince(split[0]);for (String c : cList) {String[] split2 = c.split("\\*");//System.out.println(split2[0] +" * "+split2[1]);//1301 * 石家庄市List<String> c2List = getCountriesByCity(split2[0].substring(0, 2) + "/" + split2[0]);for (String c2 : c2List) {String[] split3 = c2.split("\\*");//System.out.println(split3[0] + " * " + split3[1]);  //130324 * 卢龙县List<String> tList = getTownsByCountry(split3[0].substring(0, 2) + "/" + split3[0].substring(2, 4) + "/" + split3[0]);for (String t : tList) {String[] split4 = t.split("\\*");if(split4[0].length()!=3){//System.out.println(split4[0] + " * " + split4[1]); // 140802204 * 上郭乡List<String> vList = getVillagesByCountry(split4[0].substring(0, 2) + "/" + split4[0].substring(2, 4) + "/" + split4[0].substring(4, 6) + "/" + split4[0]);Town town = townMapper.selectOne(new QueryWrapper<Town>().eq("code", split4[0]));List<Village> list = new ArrayList<>();for (String v : vList) {String[] split5 = v.split(" ");Village village = Village.builder().name(split5[1]).code(split5[0]).townId(town.getId()) .build();list.add(village);}//System.out.println(list);int res = villageMapper.batchInsert(list);//System.out.println(res);}}}}}}
}

数据库SQL文件下载地址

https://download.csdn.net/download/lianghecai52171314/18317507

【精品】爬取 国家统计局 2020年 省市县乡村 数据相关推荐

  1. 爬取国家统计局2020年五级联动行政区划(精确)

    无其他新鲜数据的情况下,这篇应该是国家统计局专栏的最后一篇 思路和之前爬国家统计局运用的根节点叶节点思路基本相同,先放代码,具体的说明想好再解释~(代码中有部分注释) 爬到最小的村级大概用时一个半小时 ...

  2. Python3爬取国家统计局官网2019年全国所有城市(2020年更新)

    Python3爬取国家统计局官网2019年全国所有城市(2020年更新) 一级城市爬取 一级城市爬取 由于最近需要用到所有城市的数据,故从统计局爬取19年的一级城市数据 import random i ...

  3. 【Java】Java爬取国家统计局五级行政区划编码(省、市(州)、县(区)、乡(镇)、村)

    今天使用了idea+java爬取国家统计局12位行政区划编码,包括省.市(州).县(区).乡(镇).以及村委会/委员会等的行政编码和名称,将区划编码以及名称保存在数据库中. 本文内容包括数据库数据效果 ...

  4. 利用Python爬取《囧妈》豆瓣短评数据,并进行snownlp情感分析

    利用Python爬取<囧妈>豆瓣短评数据,并进行snownlp情感分析 一.电影评论爬取 今年的贺岁片<囧妈>上映前后,在豆瓣评论上就有不少网友发表了自己的观点,到底是好评的声 ...

  5. 程序员岗位介绍,我爬取了拉勾网所有技术岗位工资数据,算法工程师平均薪资高达2W

    点击观看视频 ↓↓↓ 程序员岗位介绍,我爬取了拉勾网所有技术岗位工资数据,算法工程师平均薪资高达2W 文字版 大家好,我是宁一,一个多月没有更新视频了,这一个多月我都在准备一个店铺商城的云开发小程序项 ...

  6. python爬百度翻译-Python爬取百度翻译(利用json提取数据)

    本篇文章给大家带来的内容是关于Python爬取百度翻译(利用json提取数据),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 工具:Python 3.6.5.PyCharm开发工具. ...

  7. python爬取地图地址_python爬取了高德地图一些地点的数据,爬出来数据大致情况如下:...

    python爬取了高德地图一些地点的数据,爬出来数据大致情况如下: 下面是基本流程: 1.注册成为高德地图API开发者,网址http://lbs.amap.com/(主要是获取自己的keywords ...

  8. 4ye含泪用python爬取了自己的公众号粉丝数据

    4ye含泪用python爬取了自己的公众号粉丝数据 小伙伴们好呀,最近本来是在捣鼓Gateway的知识点的,结果被一件事情搞得心不在焉 哈哈哈哈,结果不得不先鸽下~ 搞完这件事情再继续哦!! ε=ε= ...

  9. python爬虫爬取实习僧岗位信息并存入excel数据表中

    欢迎访问我的个人网站http://liubofeng.com 网页分析 博主在本博客中爬取的是数据分析岗位. 进入网站首页https://www.shixiseng.com/搜索数据分析,F12审查元 ...

  10. 网络爬虫-爬取有效机构查询网(CNAS)全量数据

    目标网站 --> 有效机构查询网 需求如下: 咋一看 不太难 全是静态html 且数据也都是规则的 这时候xpath工程师的作用就体现了! 于是乎先开始抓接口 查询接口 这里出现了个验证码 而且 ...

最新文章

  1. SAP SD基础知识之物料确定(Material Determination)
  2. git没有changId解决方法
  3. iOS如何转换十三位的时间戳
  4. 自制Android相机
  5. C语言再学习 -- 详解C++/C 面试题 2
  6. 超实用资源,SCI写作到投稿全阶段模板
  7. java ueditor 图片上传加水印_Ueditor编辑器上传图片加水印【亲测可用】-帝国CMS整合...
  8. 数据结构---模式匹配
  9. java8 streams_当Java 8 Streams API不够用时
  10. 这本书人手一本,杠精能少97%?
  11. python解acm题_python ACM ,持续更新中。。。。。。。。。。。。
  12. 多进程和多线程的区别_关于多进程和多线程的那些事儿
  13. idea自动整理代码快捷键_MDK进阶使用教程,快捷启动任意软件,自动整理格式化代码,方便代码整理可以很好 的提高效率...
  14. html在线取色,JS实现的RGB网页颜色在线取色器完整实例
  15. 小米笔记本pro频繁蓝屏故障解决
  16. 华为设备MAC地址配置命令
  17. 计算机网易云不能一键升级音质,网易云电脑版怎么调音效(一览网易云音效最佳调节技巧)...
  18. EXCEL实现按照奇偶行填充
  19. 机器学习——葡萄酒分类问题(输入与输出维度相同)
  20. WPF TextBlock Trimming

热门文章

  1. mysql大表修改字段_mysql 如何给大表添加字段
  2. PMP课程笔记:第1-3章 引论 项目运行环境 项目经理
  3. openwrt1907使用mt7621+mt715 5G wifi吞吐量低问题解决方法
  4. 汉字查拼音微信小程序项目源码
  5. 信号公式汇总之Z变换
  6. W3school练习
  7. VIVADO 下载mcs 文件
  8. 尽在双11:阿里巴巴技术演进与超越
  9. python计算条件概率_统计算法_概率基础
  10. 编写树莓派引脚驱动代码