2014最新行政区划数据采集 有源码

最近一新项目要用到国内行政区划数据,bing了一下,已有网友提供sql版本数据下载,但在本地查看数据不够新,至少我老家所在市2010年改名儿了这数据也看不到。所以说呢还是自己动手丰衣足食。然后就有了这篇文章

一、从国家统计局网站找到最新行政区划代码

see 中华人民共和国国家统计局>>行政区划代码

数据源找到了,拿回本地也简单,HttpWebRequest GET一次搞定。

二、分析HTML代码,找到区分省市区的关键

通过查看html代码,可以找到很明显的规律.

<p class="MsoNormal" align="justify">110000&nbsp;&nbsp;&nbsp; 北京市</p>
<p class="MsoNormal" align="justify">110100&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 市辖区</p>
<p class="MsoNormal" align="justify">110101&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;东城区</p>

不难发现,&nbsp;的个数决定了当前元素是省市还是区

Code

string[] lines = html.Split(new string[] { "</p>" }, StringSplitOptions.RemoveEmptyEntries);
string code = null, name= null,line = null;

List<Node> nodes = new List<Node>();
Node PrevCity = null;
Node PrevProvince = null;
for (int i = 0; i < lines.Length; i++)
{
    Node nod = new Node();
    line = ExtractHtml(lines[i], "align=\"justify\">", "");
    code = line.Substring(0, line.IndexOf("&"));
    name = line.Substring(line.LastIndexOf(";")+1).Trim();
    nod.code = code;
    nod.name = name;

    int timesOfSpaceOccure = CountString(line, "&nbsp;");
    nod.spaces = timesOfSpaceOccure;
    if (timesOfSpaceOccure == 3)
    {
        nodes.Add(nod);
        PrevProvince = nod;
        PrevCity = null;
    }
    else
    {
        if (timesOfSpaceOccure > PrevProvince.spaces)
        {
            //下一级别
            if (PrevCity != null && timesOfSpaceOccure > PrevCity.spaces)
            {
                if (PrevCity.cell == null)
                {
                    PrevCity.cell = new List<Node>();
                }
                PrevCity.cell.Add(nod);
            }
            else
            {
                //市
                if (PrevProvince.cell == null)
                {
                    PrevProvince.cell = new List<Node>();
                }
                PrevProvince.cell.Add(nod);
                PrevCity = nod;
            }
        }
    }
    
}

输出样例

[{"code":"110000","name":"北京市","cell":[
	{"code":"110100","name":"市辖区","cell":[
		{"code":"110101","name":"东城区"},
		{"code":"110102","name":"西城区"}
		]
	}]
},{"code":"440000","name":"广东省","cell":[
	{"code":"440300","name":"深圳市","cell":[
		{"code":"440301","name":"市辖区"},
		{"code":"440303","name":"罗湖区"}
		]
	}]
}
]

行政区划json文件下载

采集源码下载

不要问我行政区划里的“市辖区” 是什么意思,哥也不知道啊

posted @ 2014-10-28 08:54  码尔代夫iimax  阅读(3063)  评论(1编辑  收藏  举报