Java读取国家统计局乡镇街道数据

将乡镇街道数据写入到sql,方便导入数据库表;

package com.mock.common;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.hutool.core.io.FileUtil;

import com.mock.entity.AreaEntity;


public class AreaSqlUtils {
    public static String TJSJ_URL = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2021";

    public static void main(String[] args) { 
        getProvince();
    }
    private static void getProvince() { 
        Document doc;
        try { 
            doc = Jsoup.connect(TJSJ_URL).get();
            Elements links = doc.select("tr.provincetr").select("a");
            AreaEntity param = null;
            for (Element e : links) { 
                param = new AreaEntity();
                String href = e.attr("href");
                String[] arr = href.split("\\.");
                String code = arr[0];
				if (arr[0].length() < 6) { 
                    for (int i = 0; i < 6 - arr[0].length(); i++) { 
                        code += "0";
                    }
                 }
                param.setCode(code);
                param.setName(e.text());
                String absHref = e.attr("abs:href");
                System.out.println(absHref);
                getCity(absHref, param);

                try { 
                    Thread.sleep(1000);
                } catch (InterruptedException e1) { 
                    e1.printStackTrace();
                }
            }
        } catch (IOException e) { 
            e.printStackTrace();
        }
    }

    private static void getCity(String url, AreaEntity param) { 
        Document doc;
        try { 
            doc = Jsoup.connect(url).get();

            Elements links = doc.select("tr.citytr");
            AreaEntity city;
            for (Element e : links) { 
                city = new AreaEntity();
                Elements alist = e.select("a");
                Element codeE = alist.get(0);
                Element codeN = alist.get(1);
                String name = codeN.text();
                String code = codeE.text();
                if ("市辖区".equals(name)) { 
                    name = param.getName();
                }
                city.setCode(code);
                city.setName(name);
                String absHref = codeE.attr("abs:href");
                getArea(absHref, param, city);
            }
        } catch (IOException e) { 
            e.printStackTrace();
        }
    }
    /** * 获取区县地址 * @param url * @param param */
    private static void getArea(String url,AreaEntity province, AreaEntity city) { 
        Document doc;
        try { 
            doc = Jsoup.connect(url).get(); // Jsoup.connect(url).get();
            Elements links = doc.select("tr.countytr");
            AreaEntity area;
            for (Element e : links) { 
                area = new AreaEntity();
                Elements alist = e.select("a");
                if (alist.size() > 0) { 
                    Element codeE = alist.get(0);
                    String code = codeE.text();
                    area.setCode(code);
                    Element codeN = alist.get(1);
                    String name = codeN.text();
                    area.setName(name);
                    String absHref = codeE.attr("abs:href");
                    getTown(absHref, province, city, area);

                }
            }
        } catch (IOException e) { 
            e.printStackTrace();
        }
    }
    //乡镇
    private static void getTown(String url, AreaEntity province, AreaEntity city, AreaEntity area) { 
        List<String> list = new ArrayList<String>();
        Document doc;
        try { 
            doc = Jsoup.connect(url).get();

            Elements links = doc.select("tr.towntr");
            AreaEntity town;
            for (Element e : links) { 
                town = new AreaEntity();
                Elements alist = e.select("a");
                if (alist.size() > 0) { 
                    Element codeE = alist.get(0);
                    String code = codeE.text();
                    town.setCode(code);
                    Element codeN = alist.get(1);
                    String name = codeN.text();
                    town.setName(name);
                    String line = "INSERT INTO tbl_area(code, province, city, area, town) VALUES('" + code +"', '"+ province.getName()+"', '" + city.getName()
                            + "', '" + area.getName() + "', '"+name + "');";
                    list.add(line);
                } 
            }
            FileUtil.appendUtf8Lines(list, "F:\\test.sql");
        } catch (IOException e) { 
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值