根据[国家统计局最新行政区规划]数据库代码
根据国家统计局最新行政区规划获取省市区县镇村等数据库代码
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); // 省级无父节点,设为 0
provinceArea.setPinyin(convertToPinyin(provinceName)); // 省级无父节点,设为 0
areas.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: 获取区级数据,拼接URL
String 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);
}