基于jsoup獲取全國省市區區域編碼
本文獲取全國省市區區域編碼路徑:http://www.mca.gov.cn//article/sj/xzqh/2020/202006/202008310601.shtml


文章目錄
- 基于jsoup獲取全國省市區區域編碼
- 前言
- 一、jsoup是什么?
- 二、使用步驟
- 1.引入庫
- 2.ChinaRegionsInfo.JAVA 物體類
- 3.爬取省市區區域代碼實體
- 總結
前言
省市區區域編碼
一、jsoup是什么?
jsoup是決議HTML得
二、使用步驟
1.引入庫
<!--決議HTML-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
2.ChinaRegionsInfo.JAVA 物體類
public class ChinaRegionsInfo {
/**
* 行政區域編碼
*/
private String code;
/**
* 行政區域名稱
*/
private String name;
/**
* 行政區域型別,1:省份,2:城市,3:區或者縣城
*/
private Integer type;
/**
* 上一級行政區域編碼
*/
private String parentCode;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public String getParentCode() {
return parentCode;
}
public void setParentCode(String parentCode) {
this.parentCode = parentCode;
}
@Override
public String toString() {
return "ChinaRegionsInfo{" +
"code='" + code + '\'' +
", name='" + name + '\'' +
", type=" + type +
", parentCode='" + parentCode + '\'' +
'}';
}
}
3.爬取省市區區域代碼實體
//需要抓取的網頁地址
private static final String URL = "http://www.mca.gov.cn//article/sj/xzqh/2020/202006/202008310601.shtml";
public static void main(String[] args) throws IOException {
List<ChinaRegionsInfo> regionsInfoList = new ArrayList<>();
//抓取網頁資訊
Document document = Jsoup.connect(URL).get();
//獲取真實的資料體
Element element = document.getElementsByTag("tbody").get(0);
String provinceCode = "";//省級編碼
String cityCode = "";//市級編碼
if (Objects.nonNull(element)) {
Elements trs = element.getElementsByTag("tr");
for (int i = 3; i < trs.size(); i++) {
Elements tds = trs.get(i).getElementsByTag("td");
if (tds.size() < 3) {
continue;
}
Element td1 = tds.get(1);//行政區域編碼
Element td2 = tds.get(2);//行政區域名稱
if (StringUtils.isNotEmpty(td1.text())) {
if (td1.classNames().contains("xl7030796")) {
if (td2.toString().contains("span")) {
//市級
ChinaRegionsInfo chinaRegions = new ChinaRegionsInfo();
chinaRegions.setCode(td1.text());
chinaRegions.setName(td2.text());
chinaRegions.setType(2);
chinaRegions.setParentCode(provinceCode);
regionsInfoList.add(chinaRegions);
cityCode = td1.text();
} else {
//省級
ChinaRegionsInfo chinaRegions = new ChinaRegionsInfo();
chinaRegions.setCode(td1.text());
chinaRegions.setName(td2.text());
chinaRegions.setType(1);
chinaRegions.setParentCode("");
regionsInfoList.add(chinaRegions);
provinceCode = td1.text();
}
} else {
//區或者縣級
ChinaRegionsInfo chinaRegions = new ChinaRegionsInfo();
chinaRegions.setCode(td1.text());
chinaRegions.setName(td2.text());
chinaRegions.setType(3);
chinaRegions.setParentCode(StringUtils.isNotEmpty(cityCode) ? cityCode : provinceCode);
regionsInfoList.add(chinaRegions);
}
}
}
}
//列印結果
System.out.println(JSONArray.toJSONString(regionsInfoList));
}
總結
本文只是一個簡單得獲取省市區區域編碼得案例!非原創!原創是誰俺也忘了!俺很久看見了一個博文!通過那個博文開啟了俺jsoup得啟蒙!感謝!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/244826.html
標籤:java
上一篇:Java開發入門
