├── 2018_addr.tsv.gz ├── run.sh ├── conf └── parsers │ ├── addr.json │ ├── addr_year.json │ ├── province.json │ ├── city.json │ ├── county.json │ └── town.json ├── README.md ├── process.py └── 2018_addr_sample.tsv /2018_addr.tsv.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liuzl/china-address-code/HEAD/2018_addr.tsv.gz -------------------------------------------------------------------------------- /run.sh: -------------------------------------------------------------------------------- 1 | go get github.com/crawlerclub/bcrawler 2 | bcrawler -start addr_year -sleep 2 -log_dir ./log -alsologtostderr 3 | -------------------------------------------------------------------------------- /conf/parsers/addr.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "addr", 3 | "example_url": "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/", 4 | "default_fields": true, 5 | "rules": { 6 | "root": [ 7 | { 8 | "type": "url", 9 | "key": "addr_year", 10 | "xpath": "//div[@class='center_list']//a" 11 | } 12 | ] 13 | }, 14 | "js": "" 15 | } 16 | -------------------------------------------------------------------------------- /conf/parsers/addr_year.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "addr_year", 3 | "example_url": "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/index.html", 4 | "default_fields": true, 5 | "rules": { 6 | "root": [ 7 | { 8 | "type": "dom", 9 | "key": "province", 10 | "xpath": "//table[@class='provincetable']//a" 11 | } 12 | ], 13 | "province": [ 14 | { 15 | "type": "url", 16 | "key": "province", 17 | "xpath": "." 18 | }, 19 | { 20 | "type": "text", 21 | "key": "name", 22 | "xpath": "." 23 | } 24 | ] 25 | }, 26 | "js": "" 27 | } 28 | -------------------------------------------------------------------------------- /conf/parsers/province.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "province", 3 | "example_url": "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/13.html", 4 | "default_fields": true, 5 | "rules": { 6 | "root": [ 7 | { 8 | "type": "dom", 9 | "key": "city", 10 | "xpath": "//table[@class='citytable']//tr[position()>1]" 11 | } 12 | ], 13 | "city": [ 14 | { 15 | "type": "url", 16 | "key": "city", 17 | "xpath": "./td[1]/a" 18 | }, 19 | { 20 | "type": "text", 21 | "key": "code", 22 | "xpath": "./td[1]" 23 | }, 24 | { 25 | "type": "text", 26 | "key": "name", 27 | "xpath": "./td[2]" 28 | } 29 | ] 30 | }, 31 | "js": "" 32 | } 33 | -------------------------------------------------------------------------------- /conf/parsers/city.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "city", 3 | "example_url": "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/13/1305.html", 4 | "default_fields": true, 5 | "rules": { 6 | "root": [ 7 | { 8 | "type": "dom", 9 | "key": "county", 10 | "xpath": "//table[@class='countytable']//tr[position()>1]" 11 | } 12 | ], 13 | "county": [ 14 | { 15 | "type": "url", 16 | "key": "county", 17 | "xpath": "./td[1]/a" 18 | }, 19 | { 20 | "type": "text", 21 | "key": "code", 22 | "xpath": "./td[1]" 23 | }, 24 | { 25 | "type": "text", 26 | "key": "name", 27 | "xpath": "./td[2]" 28 | } 29 | ] 30 | }, 31 | "js": "" 32 | } 33 | -------------------------------------------------------------------------------- /conf/parsers/county.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "county", 3 | "example_url": "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/13/05/130524.html", 4 | "default_fields": true, 5 | "rules": { 6 | "root": [ 7 | { 8 | "type": "dom", 9 | "key": "town", 10 | "xpath": "//table[@class='towntable']//tr[position()>1]" 11 | } 12 | ], 13 | "town": [ 14 | { 15 | "type": "url", 16 | "key": "town", 17 | "xpath": "./td[1]/a" 18 | }, 19 | { 20 | "type": "text", 21 | "key": "code", 22 | "xpath": "./td[1]" 23 | }, 24 | { 25 | "type": "text", 26 | "key": "name", 27 | "xpath": "./td[2]" 28 | } 29 | ] 30 | }, 31 | "js": "" 32 | } 33 | -------------------------------------------------------------------------------- /conf/parsers/town.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "town", 3 | "example_url": "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/13/05/24/130524101.html", 4 | "default_fields": true, 5 | "rules": { 6 | "root": [ 7 | { 8 | "type": "dom", 9 | "key": "village", 10 | "xpath": "//table[@class='villagetable']//tr[position()>1]" 11 | } 12 | ], 13 | "village": [ 14 | { 15 | "type": "text", 16 | "key": "code", 17 | "xpath": "./td[1]" 18 | }, 19 | { 20 | "type": "text", 21 | "key": "class", 22 | "xpath": "./td[2]" 23 | }, 24 | { 25 | "type": "text", 26 | "key": "name", 27 | "xpath": "./td[3]" 28 | } 29 | ] 30 | }, 31 | "js": "" 32 | } 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # china-address-code 2 | 国家统计局中国省市县乡村5级地址抓取,http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/index.html 3 | 4 | 使用已经抓取处理好的数据可以直接[下载](2018_addr.tsv.gz)。 5 | 6 | ## 数据格式说明 7 | 8 | 处理好的数据是tsv格式的文本文件,第一列是区划代码,第二列是地址名称。其中地址类型根据区划代码的位数进行区分:省级2位、市级4位、县级6位、乡级9位、村级12位。 9 | 10 | ## 抓取方式 11 | 12 | 通过[github.com/crawlerclub/bcrawler](https://github.com/crawlerclub/bcrawler)进行抓取。需要系统有Golang环境,然后执行如下命令安装bcrawler: 13 | 14 | ```sh 15 | go get github.com/crawlerclub/bcrawler 16 | ``` 17 | 18 | bcrawler安装完成后,执行如下命令进行抓取: 19 | 20 | ```sh 21 | # 每隔两秒钟抓取一次,爬虫配置文件在./conf目录,抓取的数据存储在./data目录 22 | bcrawler -sleep 2 -log_dir ./log -alsologtostderr -conf ./conf 23 | ``` 24 | 25 | 抓取完成后,执行如下命令进行数据处理: 26 | 27 | ```sh 28 | python process.py > 2018_addr.tsv 29 | #gzip 2018_addr.tsv 30 | ``` 31 | 32 | ### 快速运行 33 | 34 | ```sh 35 | git clone https://github.com/liuzl/china-address-code 36 | cd china-address-code 37 | ./run.sh 38 | ``` 39 | 40 | ## 抓取配置 41 | 42 | 抓取的配置文件都在`./conf/parsers`目录,每一个json文件对应着一类页面的抓取配置。 -------------------------------------------------------------------------------- /process.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | def province(item): 4 | for p in item['province']: 5 | pos = p['province'].rfind('/') + 1 6 | print("%s\t%s" % ( 7 | p['province'][pos:].replace('.html',''), 8 | p['name'])) 9 | 10 | def x(item, name): 11 | p = 12 12 | if name == "city": p = 4 13 | elif name == "county": p = 6 14 | elif name == "town": p = 9 15 | if type(item[name]) is list: 16 | for c in item[name]: 17 | print("%s\t%s" % (c['code'][:p], c['name'])) 18 | else: 19 | print("%s\t%s" % (item[name]['code'][:p], item[name]['name'])) 20 | 21 | def process(f): 22 | for line in open(f): 23 | if line.strip() == "": continue 24 | item = json.loads(line) 25 | cls = item['from_parser_'] 26 | if cls == "addr_year": province(item) 27 | elif cls == "province": x(item, 'city') 28 | elif cls == "city": x(item, 'county') 29 | elif cls == "county": x(item, 'town') 30 | elif cls == "town": x(item, 'village') 31 | 32 | if __name__ == "__main__": 33 | import glob 34 | for f in glob.glob("./data/*.dat"): 35 | process(f) 36 | -------------------------------------------------------------------------------- /2018_addr_sample.tsv: -------------------------------------------------------------------------------- 1 | code name 2 | 11 北京市 3 | 12 天津市 4 | 13 河北省 5 | 14 山西省 6 | 15 内蒙古自治区 7 | 21 辽宁省 8 | 22 吉林省 9 | 23 黑龙江省 10 | 31 上海市 11 | 32 江苏省 12 | 33 浙江省 13 | 34 安徽省 14 | 35 福建省 15 | 36 江西省 16 | 37 山东省 17 | 41 河南省 18 | 42 湖北省 19 | 43 湖南省 20 | 44 广东省 21 | 45 广西壮族自治区 22 | 46 海南省 23 | 50 重庆市 24 | 51 四川省 25 | 52 贵州省 26 | 53 云南省 27 | 54 西藏自治区 28 | 61 陕西省 29 | 62 甘肃省 30 | 63 青海省 31 | 64 宁夏回族自治区 32 | 65 新疆维吾尔自治区 33 | 1301 石家庄市 34 | 1302 唐山市 35 | 1303 秦皇岛市 36 | 1304 邯郸市 37 | 1305 邢台市 38 | 39 | 2301 哈尔滨市 40 | 2302 齐齐哈尔市 41 | 2303 鸡西市 42 | 2304 鹤岗市 43 | 2305 双鸭山市 44 | 2306 大庆市 45 | 2307 伊春市 46 | 2308 佳木斯市 47 | 2309 七台河市 48 | 2310 牡丹江市 49 | 130527 南和县 50 | 130528 宁晋县 51 | 130529 巨鹿县 52 | 130530 新河县 53 | 130531 广宗县 54 | 130532 平乡县 55 | 130533 威县 56 | 130534 清河县 57 | 130535 临西县 58 | 130571 河北邢台经济开发区 59 | 130581 南宫市 60 | 130582 沙河市 61 | 130601 市辖区 62 | 130602 竞秀区 63 | 130606 莲池区 64 | 130607 满城区 65 | 130608 清苑区 66 | 130609 徐水区 67 | 130623 涞水县 68 | 130624 阜平县 69 | 350926100 双城镇 70 | 350926101 富溪镇 71 | 350926200 城郊乡 72 | 350926203 黄柏乡 73 | 350926204 宅中乡 74 | 350926206 英山乡 75 | 350981001 城南街道 76 | 350981002 城北街道 77 | 350981003 阳头街道 78 | 350981004 罗江街道办事处 79 | 350981100 赛岐镇 80 | 350981101 穆阳镇 81 | 350981102 上白石镇 82 | 350981103 潭头镇 83 | 350981104 社口镇 84 | 350981105 晓阳镇 85 | 350981106 溪潭镇 86 | 130923105212 大油周庄村委会 87 | 130923105213 东油周庄村委会 88 | 130923105214 张申庄村委会 89 | 130923105215 北肖庄村委会 90 | 130923105216 仓一村委会 91 | 130923105217 仓二村委会 92 | 130923105218 仓三村委会 93 | 130923105220 仓于村委会 94 | 130923105221 柴庄村委会 95 | 431302004008 扶青社区 96 | 431302004009 甘子冲社区 97 | 431302004010 丹阳社区 98 | 431302005001 大科社区 99 | 431302005003 罗家社区 100 | 431302005004 黄泥社区 101 | 431302005007 南垅社区 102 | --------------------------------------------------------------------------------