Coder Social home page Coder Social logo

snakejordan / administrative-divisions-of-china-on-python Goto Github PK

View Code? Open in Web Editor NEW
95.0 5.0 64.0 34 KB

中华人民共和国行政区划爬虫,分为省级、地级、县级、乡级、村级,最多五级连动,可通过本项目自行爬取数据也可使用本项目在线接口直接调用。

Home Page: https://pcctv.public.ty289.com/

License: GNU General Public License v3.0

Python 100.00%
china-region china-city china-area china-area-data python python3 address province city county

administrative-divisions-of-china-on-python's Introduction

中华人民共和国行政区划数据

license author

环境要求

  • macOS or Linux or Windows
  • python (3.6+)
  • pip
  • sqlite3

依赖包

requests_html pyquery sshtunnel redis

数据来源

数据来源说明:

中华人民共和国行政区划官方数据分为两个渠道(本人已知渠道,如有其它官方渠道可通过 issues 进行交流),分别为民政部统计局,本项目采用统计局数据。

数据来源区别:

  • 民政部:

    • 数据历史久远,包括从 1980 年至今行政区划数据;
    • 数据层级较少,最多分为省、市、区三级数据。
  • 统计局:

    • 数据历史较新,包括从 2009 年至今行政区划数据;
    • 数据层级较多,最多分为省级、地级、县级、乡级、村级五级数据。

本项目数据源:

为了数据更详细,所以采用统计局数据,具体数据源链接如下:

统计用区划代码和城乡划分代码编制规则:

参考:统计用区划代码和城乡划分代码编制规则

配置文件

# 为 True 会详细显示操作中的每句日志内容。
SHOW_LOG = True

# 爬虫每次爬取后的休眠时间,单位为秒,正常情况下无需休眠。
CRAWLER_SLEEP_TIME = 0

# csv 输出文件的字符编码,默认为 UTF-8,为了 Microsoft Office Excel 可以正常显示可以设置为 GBK,但是 GBK 可能会出现字符编码异常导致程序运行失败。
CSV_OUTPUT_FILE_ENCODING = 'UTF-8'

# Redis 地址
REDIS_HOST = '127.0.0.1'
# Redis 端口
REDIS_PORT = 6379
# Redis 密码
REDIS_PASS = ''
# Redis 库号
REDIS_DB = 3

# Redis 服务器在本地不可访问的远程服务器时,可通过配置 ssh 隧道的方式访问 Redis,从而保存数据到远程 Redis 服务器。
# SSH 隧道地址
SSH_HOST = ''
# SSH 隧道端口
SSH_PORT = 22
# SSH 隧道用户名
SSH_USERNAME = ''
# SSH 隧道密码
SSH_PASSWORD = ''
# SSH 隧道证书文件(完整物理路径)
SSH_PKEY = ''
# SSH 隧道绑定地址
SSH_BIND_HOST = '127.0.0.1'
# SSH 隧道绑定端口
SSH_BIND_PORT = 6379

数据格式

-- 省级
CREATE TABLE `province` (`statistical_code` CHAR(12) PRIMARY KEY, `code` CHAR(2), `name` VARCHAR(100))
-- 地级
CREATE TABLE `city` (`statistical_code` CHAR(12) PRIMARY KEY, `code` CHAR(4), `name` VARCHAR(100), `province_statistical_code` CHAR(12))
-- 县级
CREATE TABLE `county` (`statistical_code` CHAR(12) PRIMARY KEY, `code` CHAR(6), `name` VARCHAR(100), `province_statistical_code` CHAR(12), `city_statistical_code` CHAR(12))
-- 乡级
CREATE TABLE `town` (`statistical_code` CHAR(12) PRIMARY KEY, `code` CHAR(9), `name` VARCHAR(100), `province_statistical_code` CHAR(12), `city_statistical_code` CHAR(12), `county_statistical_code` CHAR(12))
-- 村级
CREATE TABLE `village` (`statistical_code` CHAR(12) PRIMARY KEY, `code` CHAR(12), `name` VARCHAR(100), `province_statistical_code` CHAR(12), `city_statistical_code` CHAR(12), `county_statistical_code` CHAR(12), `town_statistical_code` CHAR(12))

数据格式

使用说明

完成环境配置及依赖安装后,可通过运行 main.py 文件的方式运行本项目,本项目运行后采用交互式命令行进行交互提示。

运行命令:

$ python3 main.py

功能列表:

  • 抓取统计局信息并保存入库。(输入1)
  • 导出统计局信息中所有省、地、县、乡、村数据的 csv 版本。(输入2)
  • 导出统计局信息中所有省、地、县、乡、村数据的 json 版本。(输入3)
  • 导出统计局信息中所有省、地、县、乡、村数据到 Redis。(输入4)

运行示例:

运行示例

在线接口

五级在线接口地址:

请求参数:

除省级无需请求参数以外,其它四级均需要名称为 topCode 的上级编号作为请求参数,topCode 的值为上一级数据的 statistical_code 值。

请求方法:

支持 GET POST PUT 等请求方法,支持 XHR fetch 等请求方式。

Postman 示例:

省级示例:

省级示例

地级示例:

地级示例

县级示例:

县级示例

乡级示例:

乡级示例

村级示例:

村级示例

特例:

部分地区不存在五级数据,为保证一致性接口统一返回五级数据,本级数据与上级数据的 statistical_code 相同时意味着层级不足五级,需调用者进行额外处理。

例如:

广东省(440000000000) -> 东莞市(441900000000) -> 东城街道办事处(441900003000) -> 岗贝社区居民委员会(441900003001)

接口调用及返回参考下图:

东莞市省级示例:

东莞市省级示例

东莞市地级示例:

东莞市地级示例

东莞市县级示例:

东莞市县级示例

东莞市乡级示例:

东莞市乡级示例

东莞市村级示例:

东莞市村级示例

下载数据

已完成以下年份(2009、2010、2011、2012、2013、2014、2015、2016、2017、2018)的 sqlite3 格式数据采集,需要 sqlite3 格式数据可通过邮件 [email protected] 索取。

提示:邮件回复的下载链接为 google drive 中存储的文件,是否能下载你懂得🙃。

Over

恩,以上是所有说明,后面没有要说的了😊。


特别鸣谢

本项目是受 modoodAdministrative-divisions-of-China 项目启发而编写,由于不擅长 NodeJS 以及觉得 Administrative-divisions-of-China 在数据及使用便利性上有一点点问题,所以重复造了个轮子,在此对 modood 表示特别鸣谢。

administrative-divisions-of-china-on-python's People

Contributors

snakejordan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

administrative-divisions-of-china-on-python's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.