Java 爬取国家统计局统计用区划代码和城乡划分代码

插入速度比较慢,建议修改成批量插入。
用的 Spring Boot2、MyBatis Plus(Jdbc 都行,随便你)、Junit5、okhttp、jsoup、dozer(你可以手动赋值,没几个属性)。
总共 70W+ 条数据。
代码中爬取的是 2017 年的,2018 年的已经出来了,尚未测试。

import com.sdkj.Application;
import com.sdkj.entity.AdministrativeDivision;
import com.sdkj.service.IAdministrativeDivisionService;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.collections4.CollectionUtils;
import org.dozer.DozerBeanMapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
import java.util.concurrent.TimeUnit;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Application.class)
public class AdministrativeDivisionTest1 {

    @Autowired
    private IAdministrativeDivisionService administrativeDivisionService;
    @Autowired
    private DozerBeanMapper dozerBeanMapper;

    private static final String INDEX_URL = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2017/";
    private static final String CHARSET_NAME_GB2312 = "GB2312";
    private static final String CHARSET_NAME_GBK = "GBK";
    private static final String[] CLASS_NAMES = {".citytr", ".countytr", ".towntr", ".villagetr"};

    @Test
    void test() throws IOException, InterruptedException {
        InputStream inputStream = new URL(INDEX_URL).openStream();
        Document doc = Jsoup.parse(inputStream, CHARSET_NAME_GB2312, INDEX_URL);
        inputStream.close();
        Elements provinceElements = doc.select(".provincetr");

        List<Map> privinceList = new LinkedList<>();
        Map privinceMap;
        for (Element provinceElement : provinceElements) {
            Elements privinceLinks = provinceElement.select("a");
            for (Element privinceLink : privinceLinks) {
                privinceMap = new LinkedHashMap();
                privinceMap.put("name", privinceLink.text());

                List<Map> childList;
                while (true) {
                    // 递归获取 Child
                    getChild(privinceMap, INDEX_URL + privinceLink.attr("href"), 0);
                    childList = (List<Map>) privinceMap.get("child");
                    // 莫名其妙,不知道为什么会出现 childList 为空的情况。
                    if (CollectionUtils.isNotEmpty(childList)) {
                        break;
                    }
                    System.out.println("childList 为空");
                }

                String code = childList.get(0).get("code").toString();
                privinceMap.put("code", code.substring(0, 2) + "0000000000");
                privinceList.add(privinceMap);
            }
        }

        // 递归保存
        save(privinceList, "0", 0);
    }

    /**
     * 递归保存
     *
     * @param list
     * @param parentCode
     * @param level
     * @throws InterruptedException
     */
    private void save(List<Map> list, String parentCode, int level) throws InterruptedException {
        if (level == CLASS_NAMES.length + 1) {
            return;
        }
        level += 1;

        if (list != null) {
            for (Map map : list) {
                AdministrativeDivision administrativeDivision = dozerBeanMapper.map(map, AdministrativeDivision.class);
                administrativeDivision.setParentCode(parentCode);
                administrativeDivision.setLevel( level);
                while (true) {
                    try {
                        administrativeDivisionService.insert(administrativeDivision);
                    } catch (Exception e) {
                        if ("connection holder is null".equals(e.getMessage())) {
                            TimeUnit.MINUTES.sleep(1);
                            continue;
                        }
                        break;
                    }
                    break;
                }

                save((List<Map>) map.get("child"), administrativeDivision.getCode(), level);
            }
        }
    }

    /**
     * 获取 child
     *
     * @param map
     * @param url
     * @param level
     */
    private void getChild(Map map, String url, int level) {
        if (level == CLASS_NAMES.length) {
            return;
        }
        System.out.println(url);

        Document doc;
        while (true) {
            try {
//                OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(1, TimeUnit.SECONDS).readTimeout(3, TimeUnit.SECONDS).build();
                OkHttpClient okHttpClient = new OkHttpClient();
                Request request = new Request.Builder().url(url).build();
                Response response = okHttpClient.newCall(request).execute();
                if (!response.isSuccessful()) {
                    continue;
                }
                byte[] bodyBytes = response.body().bytes();
                String bodyText = new String(bodyBytes, CHARSET_NAME_GB2312);
                if (bodyText.contains("�")) {
                    bodyText = new String(bodyBytes, CHARSET_NAME_GBK);
                }
                doc = Jsoup.parse(bodyText);
                break;
            } catch (IOException e) {
                // e.printStackTrace();
                System.out.println(e.getMessage());
            }
        }

        List<Map> childList = new LinkedList<>();
        Elements Elements = doc.select(CLASS_NAMES[level]);
        level += 1;
        Map childMap;
        for (Element element : Elements) {
            Elements links = element.select("td a");
            // 市辖区
            boolean isContinue = true;
            if (links.size() == 0) {
                links = element.select("td");
                isContinue = false;
            }

            Element codeLink = links.first();
            childMap = new LinkedHashMap();
            childMap.put("code", codeLink.text());
            childMap.put("name", links.last().text());

            if (isContinue) {
                getChild(childMap, url.substring(0, url.lastIndexOf("/") + 1) + codeLink.attr("href"), level);
            }

            childList.add(childMap);
        }
        map.put("level", level-1);
        map.put("child", childList);
    }
}

您好,可以使用Java的Jsoup库来爬取国家统计局行政区划信息。具体步骤如下: 1. 打开国家统计局行政区划页面:http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/ 2. 使用Jsoup库获取页面内容,并解析出需要的信息。 3. 遍历解析出的信息,可以将其存储到数据库或者文件中。 以下是示例代码: ```java import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { // 打开国家统计局行政区划页面 String url = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/"; Document doc = Jsoup.connect(url).get(); // 解析出需要的信息 Elements provinces = doc.select("tr.provincetr td a"); for (Element province : provinces) { String provinceName = province.text(); String provinceUrl = url + province.attr("href"); System.out.println(provinceName + " " + provinceUrl); Document provinceDoc = Jsoup.connect(provinceUrl).get(); Elements cities = provinceDoc.select("tr.citytr td a"); for (Element city : cities) { String cityName = city.text(); String cityUrl = url + city.attr("href"); System.out.println("\t" + cityName + " " + cityUrl); Document cityDoc = Jsoup.connect(cityUrl).get(); Elements counties = cityDoc.select("tr.countytr td a"); for (Element county : counties) { String countyName = county.text(); String countyUrl = url + county.attr("href"); System.out.println("\t\t" + countyName + " " + countyUrl); } } } } } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值