Code Monkey home page Code Monkey logo

nbsprc-spider's Introduction

国家统计用区划代码和城乡划分代码---源码、详细分析、数据


2019.05.25更新:

  • 添加了爬虫代码使用说明
  • 添加了依赖requirements.txt

2018.12.02更新:

  • 修改爬虫代码,添加了中山市/东莞市下面没有区级单位的异常处理
  • 页面源码的编码为GB2312,实际为GBK,因此手工指定编码为GBKIssues #2

2018.11.30更新:

  • 更新village.csv文件,按照code顺序从小到大排列,看起来更方便
  • 更新数据总结中的错误

2018.11.10更新:


一、本仓库介绍

统计局网站提供的2016年统计用区划代码和城乡划分代码(截止2016年07月31日)按照:省-市-县-镇-村这样的层次关系来组织页面。统计局的网站对于爬虫的限制也不多,我只使用一个ip就爬取全部数据,爬取的过程中请求被拒绝的情况很少。

本仓库包含:

二、如何使用

1、安装依赖

python3 -m pip install -r requirements.txt

2、一次性爬取全部数据

python3 ./Urban-and-rural-statistics-spider.py

3、居委会级数据分段爬取使用说明

居委会级数据量比较大,一次性爬取可能会出现内存不足,所以我提供了一种居委会级数据分段爬取,最后合并各段数据的方法,具体方法如下:

python3 ./Urban-and-rural-statistics-spider.py

以上爬取完成后,可以看到此目录下已经含有了town.csv文件。

df_town = pd.read_csv("town.csv",encoding = 'utf-8')
village = getVillage(df_town['link'][0:10000])

df_village = pd.DataFrame(village)
# 信息写入csv文件
df_village.to_csv('village-0.csv', sep=',', header=True, index=False)

例如:第一次设置village = getVillage(df_town['link'][0:10000])中的爬取链接为[0-10000],同时设置df_village.to_csv('village-0.csv', sep=',', header=True, index=False)中的保存文件名为village-0.csv

python3 ./Village-Spider-Test.py

完成第一段爬取后,然后再手动更改爬取链接为[10000,20000],同时保存文件名改为village-1.csv,执行以上命令,以此类推,直到全部爬取完成。

  • 全部爬取完毕后,复制各段数据到一个空目录下,在此目录下执行如下代码:
python3 ./合并多个csv文件.py

至此,数据合并完毕,得到完整的居委会级数据。

三、数据总结

截止2016年07月31日,我国共有:

  • 31个省
  • 344个市
  • 2852个区
  • 42927个街道
  • 665062个居委会

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.