Java实现获取行政区划的示例代码

这篇文章主要为大家详细介绍了如何利用Java语言实现获取行政区划的功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习游戏

背景

公司的行政区划代码有问题,有的没有街道信息,有的关联信息有误,然后找到了国家的网站国家统计局-行政区划,这个里面是包含了所有的行政信息,但是全是html页面,这个就没法自动获取了,只能去爬取这个数据了,java语音有三方类库Jsoup,他是一个仿浏览器的三方库,可以通过他来获取页面信息。

一、导入jar包

下面是笔者用到的全部jar包

	 org.apache.poipoi-ooxml3.9 com.google.guavaguava30.1.1-jre cn.hutoolhutool-json5.4.0 com.alibabafastjson1.2.44 org.jsoupjsoup1.14.3 org.apache.httpcomponentshttpclient4.5.5 org.springframework.bootspring-boot-starter-web2.5.4

这里说下,maven配置要从阿里云下载jar,若是从中央仓库将会非常的慢。

二、代码展示

这里是代码的展示,笔者是网上搜的代码改造的,不然网站有反爬,大概爬取2000条左右就会中断,笔者加了延时这样就避开了反爬(可能还有别的规避措施)。这里爬取的是4级行政区划:省、市、区县、街道

package com.cheng.controller; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; import java.io.FileOutputStream; import java.io.IOException; import java.net.ConnectException; import java.net.SocketTimeoutException; import java.util.*; /** @author pcc @version 1.0.0 @className JsoupTest @date 2023-03-02 10:39 */ public class JsoupTestPluMdm { static int i = 1; static String url1 = “http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2022”; static String url2 = “”; public static void main(String[] args) throws IOException { try{ List> listMap = new ArrayList<>(); Document document = Jsoup.connect(url1) .header("Cookie", "wzws_sessionid=oGQAAyWBMmNlMWZkgjdlZDJkMIAyMjEuMjM4LjEzMi41MA==; SF_cookie_1=15502425; wzws_cid=6e8cdc0aea81349b05c8a0b6c05cd7204b6e0f10e5a48d462175473d23abcb4891edf1ceb73464398cb1ce7e6f53999f7545dd0014a15b1fb4eec5c6cf37421f0c2b08528de36f728ec4c676ed264c7d") .get(); //获取他所有的省 Elements elements = document.select("body > table:nth-child(3) > tbody > tr:nth-child(1) > td > table > tbody > tr:nth-child(2) > td > table > tbody > tr > td > table > tbody"); //解析省的超链接 Elements elements1 = elements.select("tbody > tr > td > a"); for(int j=0;j map = new HashMap<>(); map.put(provinceCode,provinceName); listMap.add(map); } for (int i1 = 0; i1 <31; i1++) { System.out.println("**********************i********************:"+i); if(i%1000==0){ Thread.sleep(1000*60*10); } Map stringStringMap = listMap.get(i1); Iterator> iterator = stringStringMap.entrySet().iterator(); while(iterator.hasNext()){ Map.Entry entry = iterator.next(); String provinceCode = entry.getKey(); String provinceName = entry.getValue(); String index = provinceCode.substring(0,2)+".html"; SXSSFWorkbook wb = new SXSSFWorkbook(100); SXSSFSheet sheet = (SXSSFSheet) wb.createSheet(); // TODO 这里改成自己的地址即可,也可以存放到一个文件里 String enterFileName = "C:\\Users\\pcc\\Desktop\\xingzhengquhua\\"+provinceName+".xlsx"; FileOutputStream fileOut = new FileOutputStream(enterFileName); Row row = sheet.createRow(0); sheet.createRow(i).createCell(0).setCellValue(provinceCode);// id sheet.getRow(i).createCell(1).setCellValue(provinceName);// name sheet.getRow(i).createCell(2).setCellValue(""); // pid sheet.getRow(i).createCell(3).setCellValue("1"); // type i++; try { jsoupList2(url1 + "/" + index, provinceName, provinceCode, sheet); } catch (SocketTimeoutException e) { e.printStackTrace(); jsoupList2(url1 + "/" + index, provinceName, provinceCode, sheet); } catch (ConnectException e) { e.printStackTrace(); jsoupList2(url1 + "/" + index, provinceName, provinceCode, sheet); } row.createCell(0).setCellValue("id"); row.createCell(1).setCellValue("district_name"); row.createCell(2).setCellValue("pid"); row.createCell(3).setCellValue("type"); wb.write(fileOut); fileOut.close(); } } }catch (Exception e){ e.printStackTrace(); }finally { } } //市级页面 public static void jsoupList2(String url,String provinceName,String provinceCode,SXSSFSheet sheet) throws Exception { String cityName = ""; String cityCode = ""; url2 = url.replace(".html",""); Document document = Jsoup.connect(url).get(); Elements elements = document.select("body > table:nth-child(3) > tbody > tr:nth-child(1) > td > table > tbody > tr:nth-child(2) > td > table > tbody > tr > td > table > tbody"); Elements elements1 = elements.select("tbody > tr > td"); //j从2开始是因为他有个表头 统计用区划代码 名称 for (int j = 2; j  a”).toString().equals(“”)){ String text = elements1.get(j).text(); if (j % 2 == 0) { System.out.println(“市代码:” + text); sheet.createRow(i).createCell(0).setCellValue(text); } else { System.out.println(“市名称:” + text); sheet.getRow(i).createCell(1).setCellValue(text); sheet.getRow(i).createCell(2).setCellValue(provinceCode); sheet.getRow(i).createCell(3).setCellValue(“3”); i++; } }else { Elements elements2 = elements1.get(j).select(“td > a”); for (int j1 = 0; j1  table:nth-child(3) > tbody > tr:nth-child(1) > td > table > tbody > tr:nth-child(2) > td > table > tbody > tr > td > table > tbody”); Elements elements1 = elements.select(“tbody > tr > td”); String xianName = “”; String xianCode = “”; //j从2开始是因为他有个表头 统计用区划代码 名称 for (int j = 2; j  a”).toString().equals(”“)){ String text = elements1.get(j).text(); if (j % 2 == 0) { System.out.println(“县代码:” + text); sheet.createRow(i).createCell(0).setCellValue(text); } else { System.out.println(“县名称:” + text); sheet.getRow(i).createCell(1).setCellValue(text); sheet.getRow(i).createCell(2).setCellValue(cityCode); sheet.getRow(i).createCell(3).setCellValue(“3”); i++; } }else { Elements elements2 = elements1.get(j).select(“td > a”); for (int j1 = 0; j1  table:nth-child(3) > tbody > tr:nth-child(1) > td > table > tbody > tr:nth-child(2) > td > table > tbody > tr > td > table > tbody"); Elements elements1 = elements.select("tbody > tr > td"); //j从2开始是因为他有个表头 统计用区划代码 名称 for (int j = 2; j  a”).toString().equals(“”)){ String text = elements1.get(j).text(); if (j % 2 == 0) { System.out.println(“街道代码:” + text); sheet.createRow(i).createCell(0).setCellValue(text); } else { System.out.println(“街道名称:” + text); sheet.getRow(i).createCell(1).setCellValue(text); sheet.getRow(i).createCell(2).setCellValue(xianCode); sheet.getRow(i).createCell(3).setCellValue(“4”); i++; } }else { Elements elements2 = elements1.get(j).select(“td > a”); for (int j1 = 0; j1 

到此这篇关于Java实现获取行政区划的示例代码的文章就介绍到这了,更多相关Java获取行政区划内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是Java实现获取行政区划的示例代码的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java