+-
抓取国家统计局最新省、市、县、村镇 代码

话不多说,直接上代码吧:

1、省、市、县抓取地址截止2012年10月31日):

http://www.stats.gov.cn/tjbz/xzqhdm/t20130118_402867249.htm

代码:需要利用到Jsoup.jar,大家从网上google下直接下载,如果需要也可以点击下载:jsoup-1.6.2.jar

这里只是抓取到了p_index 和 p_name,full_name,需要在程序里面在更新下,这里更新的代码我就不写了,应该比较简单,递归取名字(或者oracle子查父取full_name)。

	/**
	 * @desc 从网页抓取最新省市县数据
	 * @param url 抓取网页的url地址
	 * @return 插入数据库的语句
	 */
	public static void GetP_INDEX(String url) {
		Document doc;
		if (StringUtils.isBlank(url)) {
			url = "http://www.stats.gov.cn/tjbz/xzqhdm/t20130118_402867249.htm";
		}
		try {
			doc = Jsoup.connect(url).get();
			Elements ems = doc.select(".MsoNormalTable").select("tr");
			StringBuffer sb = new StringBuffer();
			for (Element em : ems) {
				Elements tds = em.select("TD");
				String p_index = StringUtils.trimToEmpty(tds.get(0).text());
				String p_name = StringUtils.trimToEmpty(tds.get(1).text());
				String root_code = StringUtils.substring(p_index, 0, 2).concat("0000");
				String par_index = "0";
				int level = judgePindex(p_index);
				if (level == 3) {
					par_index = StringUtils.substring(p_index, 0, 4).concat("00");
				}
				if (level == 2) {
					par_index = StringUtils.substring(p_index, 0, 2).concat("0000");
				}
				String sql = "insert into base_province1 (P_INDEX,PAR_INDEX,ROOT_CODE, P_NAME) values ("
						.concat(p_index).concat(",").concat(par_index).concat(",").concat(root_code).concat(", trim('")
						.concat(p_name).concat("'));\n");
				sb.append(sql);
				System.out.println(sql);

			}
			FileUtils.writeStringToFile(new File("E://sql.sql"), sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * @desc 通过区域判断:省级、 市级、 县级用户
	 * @author Wu,Yang
	 * @return 1:省级, 2 :市级, 3:县级
	 */
	public static Integer judgePindex(String p_index) {
		Integer flag = 0;
		if (StringUtils.isBlank(p_index)) {
			return null;
		}
		if (!StringUtils.substring(p_index, 4, 6).equals("00")) {
			flag = 3;// 县
		} else if (!StringUtils.substring(p_index, 2, 4).equals("00")) {
			flag = 2;// 市
		} else {
			flag = 1;// 省
		}

		return flag;
	}


2、抓取乡、村、镇数据,抓取地址:http://www.stats.gov.cn/tjbz/cxfldm/2012/index.html

抓取前提条件:

    a、这里的抓取需要用到县级的数据,所以前提是省市县的数据已经保存到数据库中

    b、需要分析一下需要抓取页面规则

    c、下面的代码是直接在程序里面跑,保存到数据库中了,根据你的实际情况修改代码


/**
	 * @author Wu,Yang
	 * @version 2013-9-26
	 * @desc 获取国家统计局四级(村镇乡)数据,抓取2012年最新数据,备注获取省市县数据在GetHtmlTools中
	 * @url 本地调用地址:http://localhost:8080/bp_oracle/Index.do?method=getPorvince4
	 */
	public ActionForward getPorvince4(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String url_begin = "http://www.stats.gov.cn/tjbz/cxfldm/2012/";

		// String[] ls = new String[] { "340101", "340102" };
		StringBuffer error_p_index = new StringBuffer();
		BaseProvince baseProvince = new BaseProvince();
		baseProvince.setP_level(3);
		List<BaseProvince> baseProvinceList = getFacade().getBaseProvinceService().getBaseProvinceList(baseProvince);

		for (BaseProvince bp : baseProvinceList) {
			String p_index_string = String.valueOf(bp.getP_index());
			String par_full_name = bp.getFull_name();
			String s1 = StringUtils.substring(p_index_string, 0, 2);
			String s2 = StringUtils.substring(p_index_string, 2, 4);
			String url = url_begin.concat(s1).concat("/").concat(s2).concat("/").concat(p_index_string).concat(".html");
			Document doc;
			try {
				// doc = Jsoup.connect(url).get();
				doc = Jsoup.parse(new URL(url).openStream(), "GBK", url);
				Elements ems = doc.select(".towntr").select("tr");
				for (Element em : ems) {
					Elements tds = em.select("td");
					String p_index = StringUtils.trimToEmpty(tds.get(0).text());
					p_index = StringUtils.substring(p_index, 0, 9);
					String p_name = StringUtils.trimToEmpty(tds.get(1).text());
					String root_code = StringUtils.substring(p_index, 0, 2).concat("0000");
					String par_index = p_index_string;

					BaseProvince bps = new BaseProvince();
					bps.setP_index(Long.valueOf(p_index));
					bps.setP_name(p_name);
					bps.setS_name(p_name);
					bps.setPar_index(Long.valueOf(par_index));
					bps.setP_level(4);
					bps.setAlone(0);
					bps.setRoot_code(Long.valueOf(root_code));
					bps.setP_mag(0);
					bps.setIs_west(0);
					bps.setFull_name(par_full_name.concat(",").concat(p_name));
					bps.setOrder_value(0l);
					bps.setAdd_date(new Date());
					bps.setIs_del(0);
					// 根据实际情况保存到数据库中:getFacade().getBaseProvinceService().createBaseProvince(bps);

				}
				
			} catch (IOException e) {
				error_p_index.append(p_index_string).append(",");
				continue;
			}
		}
		logger.info("error_p_index:{}", error_p_index.toString());
		return null;
	}