Java實現(xiàn)獲取行政區(qū)劃的示例代碼
背景
公司的行政區(qū)劃代碼有問題,有的沒有街道信息,有的關聯(lián)信息有誤,然后找到了國家的網(wǎng)站國家統(tǒng)計局-行政區(qū)劃,這個里面是包含了所有的行政信息,但是全是html頁面,這個就沒法自動獲取了,只能去爬取這個數(shù)據(jù)了,java語音有三方類庫Jsoup,他是一個仿瀏覽器的三方庫,可以通過他來獲取頁面信息。
一、導入jar包
下面是筆者用到的全部jar包
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>30.1.1-jre</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-json</artifactId> <version>5.4.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.44</version> </dependency> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.14.3</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.5</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.5.4</version> </dependency>
這里說下,maven配置要從阿里云下載jar,若是從中央倉庫將會非常的慢。
二、代碼展示
這里是代碼的展示,筆者是網(wǎng)上搜的代碼改造的,不然網(wǎng)站有反爬,大概爬取2000條左右就會中斷,筆者加了延時這樣就避開了反爬(可能還有別的規(guī)避措施)。這里爬取的是4級行政區(qū)劃:省、市、區(qū)縣、街道
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<Map<String,String>> 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<elements1.size();j++){ // Thread.sleep(100); String s = elements1.get(j).select(“a”).attr(“href”); String provinceCode = s.replaceAll(“.html”,“”)+“0000”; System.out.println(“省代碼:” + provinceCode); String provinceName = elements1.get(j).text(); System.out.println(“省名稱:” + provinceName); Map<String,String> 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<String, String> stringStringMap = listMap.get(i1); Iterator<Map.Entry<String, String>> iterator = stringStringMap.entrySet().iterator(); while(iterator.hasNext()){ Map.Entry<String,String> 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開始是因為他有個表頭 統(tǒng)計用區(qū)劃代碼 名稱 for (int j = 2; j < elements1.size(); j++) { System.out.println("**********************i********************:"+i); if(i%1000==0){ Thread.sleep(1000*60*10); } // Thread.sleep(500); //判斷是否是超鏈接,不是超鏈接也要獲取數(shù)據(jù) if(elements1.get(j).select(“td > 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 < elements2.size(); j1++) { String text = elements2.get(j1).text(); if (j % 2 == 0) { System.out.println(“市代碼:” + text); cityCode = text; sheet.createRow(i).createCell(0).setCellValue(text); } else { System.out.println(“市名稱:” + text); cityName = text; sheet.getRow(i).createCell(1).setCellValue(text); sheet.getRow(i).createCell(2).setCellValue(provinceCode); sheet.getRow(i).createCell(3).setCellValue(“2”); i++; String s = elements2.get(j1).select(“a”).attr(“href”); //TODO 這里排除了海南的幾個市區(qū),更改為不排除任何市區(qū) if(true) { try { jsoupList3(url1 + “/” + s,cityName,cityCode,provinceName,provinceCode, sheet); } catch (SocketTimeoutException e) { e.printStackTrace(); jsoupList3(url1 + “/” + s,cityName,cityCode,provinceName,provinceCode, sheet); } catch (ConnectException e) { e.printStackTrace(); jsoupList3(url1 + “/” + s,cityName,cityCode,provinceName,provinceCode, sheet); } } } } } } } //縣級頁面 public static void jsoupList3(String url,String cityName,String cityCode,String provinceName,String provinceCode,SXSSFSheet sheet) throws Exception { 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”); String xianName = “”; String xianCode = “”; //j從2開始是因為他有個表頭 統(tǒng)計用區(qū)劃代碼 名稱 for (int j = 2; j < elements1.size(); j++) { System.out.println(“i:"+i); if(i%1000==0){ Thread.sleep(10006010); } // Thread.sleep(500); //判斷是否是超鏈接,不是超鏈接也要獲取數(shù)據(jù) if(elements1.get(j).select(“td > 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 < elements2.size(); j1++) { String text = elements2.get(j1).text(); xianName = text; if (j % 2 == 0) { xianCode = text; System.out.println(“縣代碼:” + xianCode); 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++; String s = elements2.get(j1).select(“a”).attr(“href”); try { jsoupList4(url1 + “/” +provinceCode.substring(0,2)+”/“+ s,xianName,xianCode,cityName,cityCode,provinceName,provinceCode, sheet); } catch (SocketTimeoutException e) { e.printStackTrace(); jsoupList4(url1 + “/” +provinceCode.substring(0,2)+”/“+ s,xianName,xianCode,cityName,cityCode,provinceName,provinceCode, sheet); } catch (ConnectException e) { e.printStackTrace(); jsoupList4(url1 + “/” +provinceCode.substring(0,2)+”/"+ s,xianName,xianCode,cityName,cityCode,provinceName,provinceCode, sheet); } } } } } } //街道頁面 public static void jsoupList4(String url,String xianName,String xianCode,String cityName,String cityCode,String provinceName,String provinceCode,SXSSFSheet sheet) throws Exception { 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開始是因為他有個表頭 統(tǒng)計用區(qū)劃代碼 名稱 for (int j = 2; j < elements1.size(); j++) { System.out.println("**********************i********************:"+i); if(i%1000==0){ Thread.sleep(1000*60*10); } // Thread.sleep(500); //判斷是否是超鏈接,不是超鏈接也要獲取數(shù)據(jù) if(elements1.get(j).select(“td > 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 < elements2.size(); j1++) { String text = elements2.get(j1).text(); if (j % 2 == 0) { System.out.println(“街道代碼:” + text);// TODO 這里不能截取,不然街道界別數(shù)據(jù)截不全 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++; } } } } } }
到此這篇關于Java實現(xiàn)獲取行政區(qū)劃的示例代碼的文章就介紹到這了,更多相關Java獲取行政區(qū)劃內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java創(chuàng)建對象的六種常用方式總結(jié)
作為Java開發(fā)者,經(jīng)常創(chuàng)建很多對象,你是否知道Java中創(chuàng)建對象有哪些方式呢?下面這篇文章主要給大家介紹了關于Java創(chuàng)建對象的六種常用方式,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-06-06java后臺調(diào)用HttpURLConnection類模擬瀏覽器請求實例(可用于接口調(diào)用)
這篇文章主要介紹了java后臺調(diào)用HttpURLConnection類模擬瀏覽器請求實例,該實例可用于接口調(diào)用,具有一定的實用價值,需要的朋友可以參考下2014-10-10java中BigDecimal的介紹及使用教程BigDecimal格式化及BigDecimal常見問題
BigDecimal是Java在java.math包中提供的線程安全的API類,用來對超過16位有效位的數(shù)進行精確的運算,這篇文章主要介紹了java中BigDecimal的介紹及使用,BigDecimal格式化,BigDecimal常見問題,需要的朋友可以參考下2023-08-08Activiti7與Spring以及Spring Boot整合開發(fā)
這篇文章主要介紹了Activiti7與Spring以及Spring Boot整合開發(fā),在Activiti中核心類的是ProcessEngine流程引擎,與Spring整合就是讓Spring來管理ProcessEngine,有感興趣的同學可以參考閱讀2023-03-03