国家统计局行政区划获取及入库ES实践

我们先看下最终效果:
在这里插入图片描述

1. ES索引新建
PUT administrative_division
{"mappings": {"properties": {"province": {"type": "keyword"},"province_code": {"type": "keyword"},"city": {"type": "keyword"},"city_code": {"type": "keyword"},"district": {"type": "keyword"},"district_code": {"type": "keyword"},"town": {"type": "keyword"},"town_code": {"type": "keyword"},"committee": {"type": "keyword"},"committee_code": {"type": "keyword"},"type_code": {"type": "keyword"}}},"settings": {"number_of_replicas": 0,"number_of_shards": 1}
}
2. 代码编写

此处代码找的网上大神写的个人认为较为简洁的,直接拿来用改下存储

from lxml import etree
import requests
import time
import random"""
国家统计局行政区划获取
"""
from elasticsearch import helpers, Elasticsearchdef init_es_client(es_host):es = Elasticsearch(hosts=[es_host], verify_certs=False)return eses_client = init_es_client('http://127.0.0.1:9200')actions = list()
count = 0def get_html(url):response = requests.get(url)response.encoding = "utf8"res = response.texthtml = etree.HTML(res)return htmlbase_url = "http://www.stats.gov.cn/sj/tjbz/tjyqhdmhcxhfdm/2023/"
url = base_url + "index.html"
province_html = get_html(url)
province_list = province_html.xpath('//tr[@class="provincetr"]/td')
province_code = province_list[0].xpath('//td/a/@href')
province_name = province_list[0].xpath('//td/a/text()')
province = dict(zip([p.split(".")[0] for p in province_code], province_name))actions = list()
for p_key in province.keys():url_city = base_url + p_key + ".html"time.sleep(random.randint(0, 3))city_html = get_html(url_city)if city_html is None:print("city_html is None", url_city)continuecity_code = city_html.xpath('//tr[@class="citytr"]/td[1]/a/text()')city_name = city_html.xpath('//tr[@class="citytr"]/td[2]/a/text()')city_url = city_html.xpath('//tr[@class="citytr"]/td[1]/a/@href')for c_num in range(len(city_url)):county_url = base_url + city_url[c_num]time.sleep(random.randint(0, 3))county_html = get_html(county_url)if county_html is None:print("county_html is None", county_url)continuecounty_code = county_html.xpath('//tr[@class="countytr"]/td[1]/a/text()')county_name = county_html.xpath('//tr[@class="countytr"]/td[2]/a/text()')county_url = county_html.xpath('//tr[@class="countytr"]/td[1]/a/@href')for t_num in range(len(county_url)):town_url = base_url + "/" + city_url[c_num].split('/')[0] + "/" + county_url[t_num]time.sleep(random.randint(0, 3))town_html = get_html(town_url)if town_html is None:print("town_html is None", town_url)continuetown_code = town_html.xpath('//tr[@class="towntr"]/td[1]/a/text()')town_name = town_html.xpath('//tr[@class="towntr"]/td[2]/a/text()')town_url = town_html.xpath('//tr[@class="towntr"]/td[1]/a/@href')for v_num in range(len(town_url)):code_ = town_url[v_num].split("/")[1].rstrip(".html")village_url = base_url + code_[0:2] + "/" + code_[2:4] + "/" + town_url[v_num]time.sleep(random.randint(0, 3))village_html = get_html(village_url)if village_html is None:print("village_html is None", village_url)continue# 居委村委代码village_code = village_html.xpath('//tr[@class="villagetr"]/td[1]/text()')# 居委村委城乡分类代码village_type_code = village_html.xpath('//tr[@class="villagetr"]/td[2]/text()')# 居委村委名称village_name = village_html.xpath('//tr[@class="villagetr"]/td[3]/text()')for num in range(len(village_code)):v_name = village_name[num]v_code = village_code[num]type_code = village_type_code[num]info = dict()info['province'] = str(p_key).ljust(12, '0')info['province_code'] = province[p_key]info['city_code'] = city_code[c_num]info['city'] = city_name[c_num]info['district_code'] = county_code[t_num]info['district'] = county_name[t_num]info['town_code'] = town_code[v_num]info['town'] = town_name[v_num]info['type_code'] = type_codeinfo['committee_code'] = v_codeinfo['committee'] = v_nameaction = {"_op_type": "index","_index": "administrative_division","_id": v_code,"_source": info}actions.append(action)if len(actions) == 10:helpers.bulk(es_client, actions)count += len(actions)print(count)actions.clear()
if len(actions) > 0:helpers.bulk(es_client, actions)count += len(actions)print(count)actions.clear()

好了,每年更新一次,慢慢跑着吧,当然我们没有考虑历史变更情况,欢迎关注公众号 算法小生,获取第一资讯

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/610206.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MATLAB有限元结构动力学分析与工程应用-徐斌|【PDF电子书+配套Matlab源码】

专栏导读 作者简介:工学博士,高级工程师,专注于工业软件算法研究本文已收录于专栏:《有限元编程从入门到精通》本专栏旨在提供 1.以案例的形式讲解各类有限元问题的程序实现,并提供所有案例完整源码;2.单元…

SQL Serve---查询

概要 1、order by子句 —默认asc(升序)、desc(降序) 2、distinct关键字 3、group by子句 4、聚合函数 —max()、min()、sum()、avg()、count() 5、having子句 6、compute子句 英文关键字 order by 排序 asc…

day16 java 包装类

包装类 包装类 : Java给每种基本数据类型都创建了一个对应的类 该类叫作包装类。 包装类的作用: 包装类可以弥补基本数据类型在面向对象的环境中的局限性和便利性。 自动拆箱 :将包装类的类型转成基本数据类型。 自动装箱 :将基本数据类型转…

卫星遥感影像统计农业产量、作物分类及面积

卫星遥感技术的广泛应用为农业领域带来了巨大的变革,其中,卫星遥感影像在农业产量估算方面的应用正成为一项关键技术。通过高分辨率的遥感数据,农业生产者可以更准确、及时地了解农田状况,实现精准农业管理,提高产量和…

飞腾银河麒麟(ARM架构)离线安装MySql8.0.28版本

下载安装包 下载地址:https://downloads.mysql.com/archives/community/ 解压后上传到服务器(或者直接上传到服务器用tar -zxvf xxx.tar命令解压) 卸载mariadb 卸载命令:yum remove mariadb-server mariadb 检查是否还有未删除的包: rpm -…

最近一些前端面试问题整理

最近一些前端面试问题整理 4月8号1. TS 中的 类型别名 和接口的区别是什么?2. 什么是深拷贝和浅拷贝?深浅拷贝的方法有哪些?浅拷贝(Shallow Copy)深拷贝(Deep Copy)区别总结 3. 使用 JSON.strin…

python|pandas的loc

159就是一行。 ac就是那两行

Gateway的简单介绍和使用

1、Gateway简介: Gateway 是一种 API 网关(API Gateway)技术,它作为微服务架构中的关键组件,负责为系统的外部请求与内部服务之间提供统一的接入点。Spring Cloud Gateway 是基于 Spring 生态系统实现的一个高性能、易…

自然语言处理、大语言模型相关名词整理

自然语言处理相关名词整理 零样本学习(zero-shot learning)词嵌入(Embedding)为什么 Embedding 搜索比基于词频搜索效果好? Word2VecTransformer检索增强生成(RAG)幻觉采样温度Top-kTop-p奖励模…

2024年3月文章一览

2024年3月编程人总共更新了12篇文章: 1.2024年2月文章一览 2.Programming Abstractions in C阅读笔记:p308-p311 3.Programming Abstractions in C阅读笔记:p312-p326 4.Programming Abstractions in C阅读笔记:p327-p330 5.…

编译原理 学习笔记

1、代码: (1 2) * 3 2、词法解析: 3、抽象语法树: 4、语法树递归下降求值: 先Current_Node是根节点乘号,乘号,是中缀运算符,找左子节点,是加号,加号是中缀表达式&…

实现RAG:使用LangChain实现图检索查询

你是不是有时会遇到这样的问题:你可能遇到的任何主题或问题,都有大量的文档,但是当尝试将某些内容应用于自己的用途时,突然发现很难找到所需的内容。 在这篇博文中,我们将看一下LangChain是如何实现RAG的,这…