Python获取全国所有的省、市、县、镇、村
来源:互联网 发布:专业音频剪辑软件 编辑:程序博客网 时间:2024/05/01 04:42
#!/usr/bin/python3# -*- coding: utf-8 -*-# author=He"""通过国家统计局数据获取中国所有城市列表"""import sysimport osimport refrom urllib import requestfrom bs4 import BeautifulSoupsys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/'header = { 'Cookie': 'AD_RS_COOKIE=20080917', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) \ AppleWeb\Kit/537.36 (KHTML, like Gecko)\ ' 'Chrome/58.0.3029.110 Safari/537.36'}class GetHttp: def __init__(self, url, headers=None, charset='utf8'): if headers is None: headers = {} self._response = '' try: print(url) self._response = request.urlopen(request.Request(url=url, headers=headers)) except Exception as e: print(e) self._c = charset @property def text(self): try: return self._response.read().decode(self._c) except Exception as e: print(e) return ''def provincetr(u, he, lists): # 获取全国省份和直辖市 t = GetHttp(u, he, 'gbk').text if t: soup = BeautifulSoup(t, 'html.parser') for i in soup.find_all(attrs={'class': 'provincetr'}): for a in i.find_all('a'): id = re.sub("\D", "", a.get('href')) lists[id] = {'id': id, 'name': a.text, 'pid': '0', 'pid1': '0', 'pid2': '0', 'pid3': '0', 'pid4': '0', 'code': id} # time.sleep(1 / 10) return listsdef citytr(u, he, lists): # 获取省下级市 l = lists.copy() for i in l: t = GetHttp(u+i+'.html', he, 'gbk').text if not t: continue soup = BeautifulSoup(t, 'html.parser') for v in soup.find_all(attrs={'class': 'citytr'}): id = str(v.find_all('td')[0].text) if id[0:4] not in lists.keys(): lists[id[0:4]] = {'id': id[0:4], 'name': str(v.find_all('td')[1].text), 'pid': '0', 'pid1': i, 'pid2': '0', 'pid3': '0', 'pid4': '0', 'code': id} return listsdef countytr(u, he, lists): # 获取市下级县 l = lists.copy() a = {} for i in l: t = GetHttp(u+i[0:2]+'/'+i+'.html', he, 'gbk').text if not t: continue soup = BeautifulSoup(t, 'html.parser') for v in soup.find_all(attrs={'class': 'countytr'}): id = str(v.find_all('td')[0].text) if id[0:6] not in lists.keys(): lists[id[0:6]] = {'id': id[0:6], 'name': str(v.find_all('td')[1].text), 'pid': '0', 'pid1': l[i]['pid1'], 'pid2': i, 'pid3': '0', 'pid4': '0', 'code': id} return listsdef towntr(u, he, lists): # 县下级镇 l = lists.copy() for i in l: t = GetHttp(u+i[0:2]+'/'+i[2:4]+'/'+i+'.html', he, 'gbk').text if not t: continue soup = BeautifulSoup(t, 'html.parser') for v in soup.find_all(attrs={'class': 'towntr'}): id = str(v.find_all('td')[0].text) if id[0:9] not in lists.keys(): lists[id[0:9]] = {'id': id[0:9], 'name': str(v.find_all('td')[1].text), 'pid': '0', 'pid1': l[i]['pid1'], 'pid2': l[i]['pid2'], 'pid3': i, 'pid4': '0', 'code': id} return listsdef villagetr(u, he, lists): # 镇下级村 l = lists.copy() for i in l: t = GetHttp(u+i[0:2]+'/'+i[2:4]+'/'+i[4:6]+'/'+i+'.html', he, 'gbk').text if not t: continue soup = BeautifulSoup(t, 'html.parser') for v in soup.find_all(attrs={'class': 'villagetr'}): id = str(v.find_all('td')[0].text) if id[0:12] not in lists.keys(): lists[id[0:12]] = {'id': id[0:12], 'name': str(v.find_all('td')[1].text), 'pid': '0', 'pid1': l[i]['pid1'], 'pid2': l[i]['pid2'], 'pid3': l[i]['pid2'], 'pid4': i, 'code': id} return listsp = provincetr(u=url, he=header, lists={})print('省')c = citytr(u=url, he=header, lists=p)print('市')o = countytr(u=url, he=header, lists=c)print('县')t = towntr(u=url, he=header, lists=o)print('镇')v = villagetr(u=url, he=header, lists=t)print('村')
阅读全文
0 0
- Python获取全国所有的省、市、县、镇、村
- python serial 获取所有的串口名称
- python获取所有的安装包
- python爬虫获取全国天气信息
- 全国所有的省份及城市选择
- 全国所有省份、城市的JSON
- xml格式 全国所有省市的信息
- 全国最新各省、市、县、镇、村数据库,详细到村的数据
- Python 获取Twitter用户的所有的friends和followers
- 【Python】获取两个日期之间的所有日期的列表
- python 如何获取文件夹下所有的目录。
- python 如何获取文件夹下所有的目录。
- Python 获取当前目录下所有文件夹的大小
- Python小程序:获取文本文件的所有内容
- Python小程序:获取二进制文件的所有内容
- Python小程序:获取文本文件的所有内容
- python 获取一个字符串所有大小写的情况
- python获取当前脚本下的所有方法
- Android权限适配全攻略
- activiti简单入门
- HDU6206 几何 + 高精度
- [日推荐]『Java学习者』爱学习的程序猿看过来~
- <问题解决>数据库date类型数据前端页面显示格式不正确问题
- Python获取全国所有的省、市、县、镇、村
- Centos7 添加用户及设置权限
- UI教程
- zookeeper在分布式系统中的应用
- IntelliJ-IDEA 使用技巧
- Add to List 121. Best Time to Buy and Sell Stock,我的解题思路
- PAT 乙级练习题1020. 月饼 (25)
- 浅析Java中的final关键字
- 一个深漂的租房经验心得