全国省市区数据抓取

    <dependency>
      <groupId>cn.hutool</groupId>
      <artifactId>hutool-all</artifactId>
      <version>5.7.9</version>
    </dependency>
    <dependency>
      <groupId>com.dtflys.forest</groupId>
      <artifactId>forest-spring-boot-starter</artifactId>
      <version>1.5.0</version>
    </dependency>
/**
 * @author 敖癸
 * @date 2021/8/19 - 17:16
 */
public interface GeoAtlasClient {


    @Get("https://geo.datav.aliyun.com/areas_v3/bound/geojson")
    JSONObject getChinaGeo(@Query("code") String adCode);
}
public class ChainGeo extends AbstractEntity{
    private Integer adCode;
    private String name;
    private Integer pid;

    @Enumerated(value = EnumType.STRING)
    private Level level;

    private String acRoutes;

    public enum Level{
        country,
        province,
        city,
        district
    }
}
    @Test
    void getGeo(){
        get("100000_full");
    }

    private void get(String code){
        JSONObject chinaGeo = geoAtlasClient.getChinaGeo(code);
        JSONArray features = chinaGeo.getJSONArray("features");
        List<JSONObject> featuresObjs = features.toList(JSONObject.class);
        List<ChainGeo> chainGeos = new ArrayList<>();
        featuresObjs.forEach(feature->{
            JSONObject properties = feature.getJSONObject("properties");
            String name = properties.getStr("name");
            Integer adCode = properties.getInt("adcode");
            Integer childrenNum = properties.getInt("childrenNum",0);
            String level = properties.getStr("level","country");
            JSONObject parent = properties.getJSONObject("parent");
            Integer parentCode = 0;
            if (parent != null){
                parentCode = parent.getInt("adcode");
            }
            List<Integer> acroutes = (List) properties.get("acroutes");
            String acRoutes = null;
            if (CollUtil.isNotEmpty(acroutes)){
                acRoutes = acroutes.stream().map(Object::toString).collect(Collectors.joining(","));
            }
            ChainGeo chainGeo = new ChainGeo();
            chainGeo.setName(name)
                .setPid(parentCode)
                .setLevel(Level.valueOf(level))
                .setAdCode(adCode)
                .setAcRoutes(acRoutes);
            chainGeos.add(chainGeo);
            if(childrenNum > 0){
                get(adCode+"_full");
            }
        });
        chainGeoRepository.saveAll(chainGeos);
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容