最新县及县以上行政区划代码(截止 2016 年 7 月 31 日)
url:http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201703/t20170310_1471429.html(网站 404)
更多数据
可以通过 UNPKG 下载使用
CDN
资源: https://unpkg.com/province-city-china/dist/
文件列表 | JSON | CSV | SQL | CDN |
---|---|---|---|---|
总数据(省/地/县/乡) | data.json | data.csv | data.sql | data.sql / csv / json |
省/地/县/乡层级数据 | level.json | - | - | level.json |
省级(省/直辖市/特别行政区) | province.json | province.csv | - | province.json / csv |
地级(城市) | city.json | city.csv | - | city.json / csv |
县级(区县) | area.json | area.csv | - | area.json / csv |
乡级(乡镇/街) | town.json | town.csv | - | town.json / csv |
县市区没有乡级数据 |
参考代码:http://www.thinksaas.cn/topics/0/353/353549.html
工具:jsoup
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class getpcd {
/**
* All rights Reserved, Designed By www.vps6.win
* @Title: getpcd.java
* @Package
* @Description: TODO(用一句话描述该文件做什么)
* @author: liuchao
* @date: 2017年4月27日 下午10:52:40
* @version V1.0
* @Copyright: 2017 www.vps6.win Inc. All rights reserved.
*
*/
private static final Logger log = Logger.getLogger("xzqhdm");
private static final String separator = "#";
public static void main(String[] args) throws Exception {
List<String> list = getData();
for(String s : list){
System.out.println(s);
}
saveData(list);
}
private static List<String> getData() throws Exception {
List<String> retList = new ArrayList<String>();
Map<String,String> map = new HashMap<String,String>();
Document doc = Jsoup.connect("http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201703/t20170310_1471429.html").post();
Elements ps = doc.select(".TRS_PreAppend p");
for (Element e : ps) {
Elements spans = e.select("span");
Element firstE = null;
Element secondE = null;;
if(spans.size() == 3){
//一级
firstE = spans.first();
secondE = spans.get(2);
}else{
//二级/三级
firstE = spans.get(1);
secondE = spans.get(3);
}
String key = StringUtils.trim(firstE.ownText());
map.put(key, key+separator+StringUtils.trim(secondE.ownText().replace(" ", ""))+ separator + key.substring(0, 4)+"00"+separator+"3");
if (key.endsWith("0000")) {
map.put(key, key+separator+StringUtils.trim(secondE.ownText().replace(" ", ""))+ separator+"0"+separator+"1");
//key = key.substring(0, 2);
} else if (key.endsWith("00")) {
map.put(key, key+separator+StringUtils.trim(secondE.ownText().replace(" ", ""))+ separator+key.substring(0, 2)+"0000"+separator+"2");
//key = key.substring(0, 4);
}
if(map.get(key) == null){
System.out.println(key);
}
retList.add(map.get(key));
}
return retList;
}
private static void saveData(List<String> list) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost/test?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull",
"root", "root");
conn.setAutoCommit(false);// 事务不自动提交
pstmt = conn.prepareStatement("delete from code");
int count = pstmt.executeUpdate();
log.log(Level.INFO, "___删除数据" + count);
int batch = 500;// 一次插入多少笔
int loop = list.size() / batch;// batch可以循环的次数
int last = list.size() - batch * loop;// 最后剩余
int start = 0;// list从哪开始取数据
int index = 1;// 问号参数
int allCount = 0;// 统计总共插入多少数据
for (int i = 0; i < loop; i++) {
index = 1;// 重置到第一个问号参数
pstmt = conn.prepareStatement(getPreSql(batch));
for (int j = start; j < start + batch; j++) {
String[] s = list.get(j).split(separator);
for (int k = 0; k < s.length; k++) {
pstmt.setString(index, s[k]);
index++;
}
}
count = pstmt.executeUpdate();
allCount += count;
log.log(Level.INFO, "___插入数据" + count);
start += batch;
}
if (last > 0) {
index = 1;// 重置到第一个问号参数
pstmt = conn.prepareStatement(getPreSql(last));
for (int j = start; j < list.size(); j++) {
String[] s = list.get(j).split(separator);
for (int k = 0; k < s.length; k++) {
pstmt.setString(index, s[k]);
index++;
}
}
count = pstmt.executeUpdate();
allCount += count;
log.log(Level.INFO, "___插入数据" + count);
}
log.log(Level.INFO, "___一共插入" + allCount);
conn.commit();// 提交事务
conn.setAutoCommit(true);// 还原现场
} catch (ClassNotFoundException e) {
log.log(Level.SEVERE, "___找不到MySQL驱动包" + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
log.log(Level.SEVERE, "___执行SQL异常" + e.getMessage());
e.printStackTrace();
try {
if (conn != null) {
conn.rollback();
conn.setAutoCommit(true);// 还原现场
log.log(Level.WARNING, "___回滚数据");
}
} catch (SQLException e1) {
log.log(Level.SEVERE, "___回滚异常" + e.getMessage());
e1.printStackTrace();
}
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
log.log(Level.SEVERE, "___关闭数据库连接异常" + e.getMessage());
e.printStackTrace();
}
}
}
/**
* 生成sql
*
* @return
*/
private static String getPreSql(int count) {
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO `code` (`code`, `name`,`pcode`,`type`) VALUES");
for (int i = 0; i < count; i++) {
sb.append("(?, ?, ?, ?),");
}
return sb.substring(0, sb.length() - 1);
}
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于