您的位置:首页 > 游戏 > 游戏 > 编程网校_新开传奇网站手游_媒介星软文平台官网_电脑优化大师官方免费下载

编程网校_新开传奇网站手游_媒介星软文平台官网_电脑优化大师官方免费下载

2024/11/19 3:46:55 来源:https://blog.csdn.net/Mr1shuai/article/details/142496620  浏览:    关键词:编程网校_新开传奇网站手游_媒介星软文平台官网_电脑优化大师官方免费下载
编程网校_新开传奇网站手游_媒介星软文平台官网_电脑优化大师官方免费下载

根据国家统计局最新行政区规划获取省市区县镇村等数据库代码

Gitee源代码地址

   public static List<Area> fetchAndStoreAreaData() throws IOException {List<Area> areas = new ArrayList<>();// Step 1: 获取省级数据Document provinceDoc = Jsoup.connect(BASE_URL + "index.html").get();Elements provinces = provinceDoc.select(".provincetr a");for (Element province : provinces) {String provinceName = province.text();String provinceUrl = province.attr("href");// 直接提取URL中的省级代码,不做复杂的substring操作String provinceCode = provinceUrl.replace(".html", "");Area provinceArea = new Area();provinceArea.setCode(Long.valueOf(provinceCode));provinceArea.setName(provinceName);provinceArea.setLevel(1); // 省级provinceArea.setPcode(0L); // 省级无父节点,设为 0provinceArea.setPinyin(convertToPinyin(provinceName)); // 省级无父节点,设为 0areas.add(provinceArea);// Step 2: 获取市级数据Document cityDoc = Jsoup.connect(BASE_URL + provinceUrl).get();Elements cities = cityDoc.select(".citytr");for (Element city : cities) {Elements cityElements = city.select("a");if (cityElements.size() < 2) {continue; // 跳过异常数据}String cityCode = cityElements.get(0).text();String cityName = cityElements.get(1).text();String cityUrl = cityElements.get(0).attr("href");Area cityArea = new Area();cityArea.setCode(Long.valueOf(cityCode));cityArea.setName(cityName);cityArea.setLevel(2); // 市级cityArea.setPcode(Long.valueOf(provinceCode));cityArea.setPinyin(convertToPinyin(cityName)); /// 父级为省areas.add(cityArea);// Step 3: 获取区级数据,拼接URLString districtUrl = BASE_URL + cityUrl;try {Document districtDoc = Jsoup.connect(districtUrl).get();Elements districts = districtDoc.select(".countytr");for (Element district : districts) {Elements districtElements = district.select("a");if (districtElements.size() > 0) {String districtCode = districtElements.get(0).text();String districtName = districtElements.get(1).text();Area districtArea = new Area();districtArea.setCode(Long.valueOf(districtCode));districtArea.setName(districtName);districtArea.setLevel(3); // 区级districtArea.setPcode(Long.valueOf(cityCode));districtArea.setPinyin(convertToPinyin(districtName)); // 父级为市areas.add(districtArea);// Step 4: 获取镇级数据String townUrl = BASE_URL + districtElements.get(0).attr("href");// try {//     Document townDoc = Jsoup.connect(townUrl).get();//     Elements towns = townDoc.select(".towntr");////     for (Element town : towns) {//         String townCode = town.select("a").get(0).text();//         String townName = town.select("a").get(1).text();////         Area townArea = new Area();//         townArea.setCode(Long.valueOf(townCode));//         townArea.setName(townName);//         townArea.setLevel(4); // 镇级//         townArea.setPcode(Long.valueOf(districtCode));//         townArea.setPinyin(convertToPinyin(townName));//         areas.add(townArea);////         // Step 5: 获取村级数据//         String villageUrl = BASE_URL + town.select("a").get(0).attr("href");//         try {//             Document villageDoc = Jsoup.connect(villageUrl).get();//             Elements villages = villageDoc.select(".villagetr");////             for (Element village : villages) {//                 String villageCode = village.select("td").get(0).text();//                 String villageName = village.select("td").get(2).text();////                 Area villageArea = new Area();//                 villageArea.setCode(Long.valueOf(villageCode));//                 villageArea.setName(villageName);//                 villageArea.setLevel(5); // 村级//                 villageArea.setPcode(Long.valueOf(townCode));//                 villageArea.setPinyin(convertToPinyin(villageName));//                 areas.add(villageArea);//             }//         } catch (IOException e) {//             System.out.println("Error fetching village data: " + e.getMessage());//         }//     }// } catch (IOException e) {//     System.out.println("Error fetching town data: " + e.getMessage());// }}}} catch (IOException e) {}}}return areas;// 批量插入到数据库// areaMapper.insertBatchSomeColumn(areas);}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com