人生不过短短几十年,莫虚度光阴,到老抱怨年轻的自己 ——行人

用高德提供的api获取最新的三级级联数据

高德api接口地址:https://restapi.amap.com/v3/config/district
高德接口文档地址:https://lbs.amap.com/api/webservice/guide/api/search

有了它,妈妈再也不用担心我熬夜处理省市区数据了!!

下载完的数据处理下,因为excel会自动把开头的0去掉,所以,把这两行换成文本格式,然后全局替换@为空,把它去掉就能用了
使用需要先到高德申请key,免费不要钱,不过一天限额30000,想要更高就要掏钱了,文件里有我的key,使用时请替换

 

 

package com.yhl.tos.rule.config;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * 根据高德提供api爬取省市区三级级联数据
 *
 * @author sen.yang@youhualin.com
 * @since 2021-03-22 16:24:23
 */
public class District {

    private static volatile int i=0;

    /**
     * 你要把文件存到哪
     */
    private static final String url="C:\\Users\\IT_YangSen\\Desktop\\省市区.csv";

    public static void main(String[] args)  {
        try{
            //e399a25bb21f97d040765b175c8ea8f1是我自己的key,不能用自己到高德弄一个,反正不要钱
            String jsonString = sendGet("https://restapi.amap.com/v3/config/district","subdistrict=4&key=e399a25bb21f97d040765b175c8ea8f1");
            JSONObject jsonObject = JSONObject.parseObject(jsonString);
            //获取到国家及下面所有的信息 开始循环插入,这里可以写成递归调用,但是不如这样方便查看、理解
            JSONArray countryAll = jsonObject.getJSONArray("districts");
            writeFile("ID,adcode,城市编码,级别,地名,父id,四级地址\n");
            for (int i = 0; i < countryAll.size(); i++) {
                JSONObject countryLeve0 = countryAll.getJSONObject(i);
                String citycode0 = countryLeve0.getString("citycode");
                String adcode0 = countryLeve0.getString("adcode");
                String name0 = countryLeve0.getString("name");
                String center0 = countryLeve0.getString("center");
                String country = countryLeve0.getString("level");
                int level = 3;
                if (country.equals("country")) {
                    level = 3;
                }
                //插入国家
                Integer id1 = insert(0, adcode0, citycode0, name0, center0, level, name0);
                JSONArray province0 = countryLeve0.getJSONArray("districts");

                for (int j = 0; j < province0.size(); j++) {
                    JSONObject province1 = province0.getJSONObject(j);
                    String citycode1 = province1.getString("citycode");
                    String adcode1 = province1.getString("adcode");
                    String name1 = province1.getString("name");
                    String center1 = province1.getString("center");
                    String province = province1.getString("level");
                    int level1 =3;
                    if (province.equals("province")) {
                        level1 = 1;
                    }
                    //插入省
                    Integer id2 = insert(id1, adcode1, citycode1, name1, center1, level1, name0, name1);
                    JSONArray city0 = province1.getJSONArray("districts");

                    for (int z = 0; z < city0.size(); z++) {
                        JSONObject city2 = city0.getJSONObject(z);
                        String citycode2 = city2.getString("citycode");
                        String adcode2 = city2.getString("adcode");
                        String name2 = city2.getString("name");
                        String center2 = city2.getString("center");
                        String city = city2.getString("level");
                        int level2 = 3;
                        if (city.equals("city")) {
                            level2 = 2;
                        }
                        //插入市
                        Integer id3 = insert(id2, adcode2, citycode2, name2, center2, level2, name0, name1, name2);

                        JSONArray street0 = city2.getJSONArray("districts");
                        for (int w = 0; w < street0.size(); w++) {
                            JSONObject street3 = street0.getJSONObject(w);
                            String citycode3 = street3.getString("citycode");
                            String adcode3 = street3.getString("adcode");
                            String name3 = street3.getString("name");
                            String center3 = street3.getString("center");
                            String street = street3.getString("level");
                            int level3 = 3;
                            if (street.equals("street")) {
                                level3 = 2;
                            }
                            //插入区县
                            insert(id3, adcode3, citycode3, name3, center3, level3, name0, name1, name2, name3);
                            //  JSONArray street = street3.getJSONArray("districts");
                            //有需要可以继续向下遍历

                        }

                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    /**
     * 公共的插入方法
     *
     * @param parentId  父id
     * @param citycode  城市编码
     * @param adcode    区域编码  街道没有独有的adcode,均继承父类(区县)的adcode
     * @param name      城市名称 (行政区名称)
     * @param center    地理坐标
     * @param level     等级  0 国家,1省,...依次排
     * @param mergeName 把名称拼接成全名
     * @return
     */
    public static Integer insert(Integer parentId, String citycode, String adcode, String name, String center, int level, String... mergeName) {
        String megName = "";
        for (int i = 0; i < mergeName.length; i++) {
            megName = megName + mergeName[i];
            if (i < mergeName.length - 1) {
                megName = megName + ",";
            }
        }
        i++;
        writeFile(i+","+"@"+adcode.replace("[]","")+","+"@"+citycode+","+level+","+name+","+parentId+","+megName+"\n");
        return i;
    }

    public static void writeFile(String content) {
        FileWriter writer = null;
        try {
            // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
            writer = new FileWriter(url, true);
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(writer != null){
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 向指定URL发送GET方法的请求
     *
     * @param url
     *            发送请求的URL
     * @param param
     *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
     * @return URL 所代表远程资源的响应结果
     */
    public static String sendGet(String url, String param) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
}

  

posted @ 2021-06-03 17:03  夜雨步行人  阅读(770)  评论(0编辑  收藏  举报