python爬虫区划代码表
用python爬虫区划代码表
爬虫宁夏行政区划代码表。
#爬虫国家统计局全国行政区划数据
#注意第13、14行,例如第13行URL为宁夏,则爬虫宁夏行政区划区划表,14行为全国区划代码表。
#import requests
import re
import xlsxwriter
import time
time_start=time.time()
agent={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'}
choose_ls=[depth*2 if depth<=3 else 3*(depth-1) for depth in range(1,6)]
match_level=['provincetr','citytr','countytr','towntr','villagetr']
initurl='http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2020/64.html' #爬虫银川市
#initurl='http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2020/index.html' #爬虫全国
total_dict={}
depth=0
each_root={initurl:('','')}
max_depth=5#可选,1-5分别表示省级、地级、县级、乡级、村级,
while depth<max_depth:total_count=0next_root={}for url in each_root:code_join=each_root[url][0]+'-' if depth!=0 else each_root[url][0]zone_join=each_root[url][1]+'-' if depth!=0 else each_root[url][1]change_root='/'.join(url.split('/')[:-1])+'/'while True:try:req=requests.get(url,headers=agent)req.encoding='GBK'#中文解码,text=req.texttext=text.replace('\n','\\n')special_sigh=Falseif match_level[depth] in text:match_text=re.findall(r"class='%s'>(.*?)</table"%match_level[depth],text)[0]breakelse:search=Falsefor level in range(depth,5):#东莞、中山、儋州缺县级单位,因此需要进行识别并放入下一节点存储if match_level[level] in text:match_text=re.findall(r"class='%s'>(.*?)</table"%match_level[level],text)[0]search=Truespecial_sigh=Trueprint('特殊区划:%s'%each_root[url][1])breakif search:breakelse:print('服务器繁忙')time.sleep(2)except:print('服务器繁忙')time.sleep(2)if special_sigh:next_root[url]=(code_join,zone_join)else:if depth!=0:has_tree=re.findall(r"href='(.*?)'>(\d+?)<.*?html'>(.*?)</a></td></tr>",match_text)else:base_tree=re.findall(r"href='(.*?)'>(.*?)<br/",match_text)has_tree=[(each[0],each[0].split('.html')[0],each[1]) for each in base_tree]base_no=re.findall(r"td>(\d+?)</td><td>(.*?)</td></tr>",match_text)no_tree=[(each[0],re.findall(r'<td>(.+)',each[1])[0] if 'td' in each[1] else each[1]) for each in base_no]for each in has_tree:each_dir=change_root+each[0]next_root[each_dir]=(code_join+each[1][:choose_ls[depth]],zone_join+each[2])if depth==3:if (total_count+1)%100==0:print('已爬取%d个,在路径%s处'%(total_count+1,zone_join+each[2]))else:print('在路径%s处'%(zone_join+each[2]))if no_tree:for each in no_tree:total_dict[code_join+each[0][:choose_ls[depth]]]=zone_join+each[1]if depth==4:if (total_count+1)%800==0:print('已爬取%d个,在路径%s处'%(total_count+1,zone_join+each[1]))else:print('已获取路径%s'%(zone_join+each[1]))total_count+=1depth+=1each_root=next_root
def decompose(each):if type(total_dict[each])==tuple:codelist=total_dict[each][0].split('-')namelist=total_dict[each][1].split('-')else:codelist=each.split('-')namelist=total_dict[each].split('-')if len(codelist)<depth:for i in range(len(codelist),depth):codelist.append('')namelist.append('')ziplist=list(zip(codelist,namelist))return [i for j in ziplist for i in j]
sort_name=['省级','地级','县级','乡级','村级']
real_column=[(sort_name[each]+'代码',sort_name[each]+'名称') for each in range(depth)]
flat_col=[i for each in real_column for i in each]
total_dict.update(each_root)
if depth<=3:#县级及以上数据量不大(约三千行),可以用excel存储wk=xlsxwriter.Workbook('五级联动.xlsx')sh=wk.add_worksheet('sheet1')for each in range(2*depth):sh.write(0,each,flat_col[each])totalrow=1for each in total_dict:flatlist=decompose(each)for i in range(2*depth):sh.write(totalrow,i,flatlist[i])totalrow+=1wk.close()
else:#县级往下数据较多,excel没有优势,因此写入csv存储book=open('五级联动.csv','w',encoding='utf-8')book.write(','.join(flat_col)+'\n')for each in total_dict:flatten=decompose(each)book.write(','.join(flatten)+'\n')book.close()
time_end=time.time()
rest_second=time_end-time_start
print('用时%d分%d秒'%divmod(rest_second,60))
python爬虫区划代码表相关推荐
- python爬虫练习五(补充): 2018年统计用区划代码和城乡划分代码(附代码与全部数据)
之前爬取过2017年的数据 详见 Python爬虫练习五:爬取 2017年统计用区划代码和城乡划分代码(附代码与全部数据) ,下面有评论说广东省的数据缺少了东莞与中山两个市的数据,检查网页结构发现确实 ...
- 抓取国家统计局区划、城乡划分代码的简易python爬虫实现
抓取国家统计局区划.城乡划分代码的简易python爬虫实现 免责声明 本篇文章仅用于学习交流,并不针对任何网站.软件.个人. 概要说明 本篇文章介绍一个简易python爬虫的开发,对国家统计局区划.城 ...
- 2018年统计用区划代码和城乡划分代码(截止2018年10月31日)(数据及python爬虫代码)
统计局网站的数据汇总. 细粒度,到最后一级(一般为5级,网站上少部分地区为4级). 数据编码格式为utf8,以便显示名称中的生僻字,请使用合适的文本工具打开. 这里有python爬虫代码和所需库.爬取 ...
- Python 爬虫 中国行政区划信息爬取 (初学者)
Python 爬虫 中国行政区划信息爬取 (初学者) 背景 环境准备 代码片段 1.定义地址信息对象 2.地址解析对象 2.1 获取web信息 2.2 web信息解析 2.3 区划信息提取 2.4 省 ...
- 肝了N小时,整理了100+Python爬虫项目
提到爬虫,相信绝大部分人的第一反应就是 Python,尽管其他编程语言一样能写爬虫,但在人们的印象中,爬虫似乎与 Python 绑定了一样,由此可见爬虫在 Python 中的份量. 最近小二做了个免费 ...
- 新手python爬虫100个入门项目
前言 1.断点续爬并存入MySQL (文末送读者福利) 2.秒爬,python爬虫中的多线程,多进程,协程 3.抓取百度图片唯美意境模块 4.抓取百度贴吧某话题下的所有图片 5.淘宝模拟登录 6.天猫 ...
- 肝了N小时,整理了100+Python爬虫项目(附源码)
提到爬虫,相信绝大部分人的第一反应就是 Python,尽管其他编程语言一样能写爬虫,但在人们的印象中,爬虫似乎与 Python 绑定了一样,由此可见爬虫在 Python 中的份量. 最近我做了个免费的 ...
- 关于Python爬虫原理和数据抓取1.1
为什么要做爬虫? 首先请问:都说现在是"大数据时代",那数据从何而来? 企业产生的用户数据:百度指数.阿里指数.TBI腾讯浏览指数.新浪微博指数 数据平台购买数据:数据堂.国云数据 ...
- python爬虫之Scrapy框架的post请求和核心组件的工作 流程
python爬虫之Scrapy框架的post请求和核心组件的工作 流程 一 Scrapy的post请求的实现 在爬虫文件中的爬虫类继承了Spider父类中的start_urls,该方法就可以对star ...
- python爬虫抓取信息_python爬虫爬取网上药品信息并且存入数据库
我最近在学习python爬虫,然后正好碰上数据库课设,我就选了一个连锁药店的,所以就把网上的药品信息爬取了下来. 1,首先分析网页 2,我想要的是评论数比较多的,毕竟好东西大概是买的人多才好.然后你会 ...
最新文章
- java-第十三章-类的无参方法(一)-实现客户姓名的添加和显示
- python编程语言是什么-python是什么编程语言
- Java synchronized 详解
- php ip2long 32 64位,詳談php ip2long 出現負數的原因及解決方法
- RabbitMQ消息队列———安装(一)
- slf4j mysql_能显示sql语句,没有报错,但是MySQL里没有表?还有,slf4j是必须的吗?...
- 18.nginx 服务器的代理服务
- Java并发编程实践笔记(一)
- Linux下USB转串口的驱动【转】
- QRCode.js 生成二维码(文字信息二维码)
- java随机生成中文姓名,性别,Email,手机号,住址 等
- 如何在java中实现小数点自增_java保留小数的四种实现方法
- iOS 图片编辑——缩放剪切
- 估值篇之最关键指标ROE
- 委托代理问题------The principal-agent problem
- 阿里云对象存储OSS存储照片
- [ 大道至简系列 ] 三分钟理解-1NF,2NF,3NF
- 重磅!瑞泰信息发布中国企业数字化增长行业实践白皮书——《数字化平台造就企业增长新引擎》
- UHS-II文档学习
- 万兴剪刀手去水印教程_万兴神剪手怎么去水印教程_赶快收藏吧