Get the latest region

Go to the portal area of ​​the Ministry of Civil Affairs, and then open the administrative division code under the civil affairs data.
Insert picture description here
This is the latest area code, and then save this page locally, open it with an editor, and add the following link in the header

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/2.1.1/jquery.js"></script>

Then add the following code

<script type="text/javascript">
	//http://www.mca.gov.cn/article/sj/xzqh/2020/2020/202003301019.html
 
//这个是扩展方法,用于尾部匹配
String.prototype.endWith=function(endStr){
    
    
      var d=this.length-endStr.length;
      return (d>=0&&this.lastIndexOf(endStr)==d)
    }
 
var provinceList=[];
 
var provinceItem={
    
    };
var cityItem={
    
    };
$("tbody tr").each(function(){
    
    
	var _codetxt=$(this).find("td").eq(1).text().trim();
	var _code=parseInt(_codetxt);
	var _name=$(this).find("td").eq(2).text().trim();
	if(!(_code>0)){
    
    return true;}
	if(_codetxt.endWith("0000"))
	{
    
    
		//以上个省的名义添加对应的城市
		if(typeof(cityItem.name)!="undefined")
		{
    
    
			provinceItem.city.push(cityItem);
			cityItem={
    
    };
		}
		if(provinceItem.city)
		{
    
    
			provinceList.push(provinceItem); 
		}
		provinceItem={
    
    
				        name: _name,
				        postcode: _codetxt,
				        city: []
				    }; 
	}else if(_codetxt.endWith("00"))
	{
    
    
		//将上个市数据完结掉
		if(typeof(cityItem.name)!="undefined"&&cityItem.area.length<=0)
		{
    
    
			cityItem.area.push({
    
    
                name: "市辖区",
                postcode: (parseInt(cityItem.postcode)+1)+""
            });
		}
		if(typeof(cityItem.name)!="undefined")
		{
    
    
			provinceItem.city.push(cityItem); 
		} 
		cityItem={
    
    
		            name: _name,
		            postcode: _codetxt,
		            area: []
			    };
 
	}
	else 
	{
    
    
		//直辖市
		if(provinceItem.name&&typeof(cityItem.name)=="undefined")
		{
    
    
			//区升级为city,下包含一个区/县
			provinceItem.city.push({
    
    
					            name: _name,
					            postcode: _codetxt,
					            area: [{
    
    
					                name: "区",
					                postcode: _codetxt+"_0"
					            }]
					        });
		}
		else
		{
    
    
			//区
			//console.log(_codetxt);
			areaItem={
    
    
			            name: _name,
			            postcode: _codetxt
	            };
	        cityItem.area.push(areaItem);
		}
	} 
 
});
 
console.log(provinceList);
	
</script>

After that, you can see the area you need in the browser. Just
Insert picture description here
copy it and open it in the editor. You can take what you need for the rest. The
original author also wrote it very clearly, record it yourself, thank you
https:/ /blog.csdn.net/fengyunxuanhai/article/details/107368213

Guess you like

Origin blog.csdn.net/weixin_43018356/article/details/112253407