当前位置: 首页 > wzjs >正文

佛山市手机网站建设哪家好百度开户公司

佛山市手机网站建设哪家好,百度开户公司,新能源汽车十大名牌,优化方案2022行政区划获取 获取2023年的行政区划,并以 编码: 省市区 格式保存为字典方便后续调用 注:网址可能会更新,根据最新的来 # 获取并保存行政区划代码 import requests from lxml import etree import jsondef fetch_html(url):""&quo…

行政区划获取

获取2023年的行政区划,并以 编码: 省市区 格式保存为字典方便后续调用
注:网址可能会更新,根据最新的来

# 获取并保存行政区划代码
import requests
from lxml import etree
import jsondef fetch_html(url):"""获取网页内容"""try:response = requests.get(url)response.raise_for_status()response.encoding = 'utf-8'return response.textexcept requests.RequestException as e:print(f"请求失败: {e}")return Nonedef parse_html(html):"""解析HTML并生成行政区划字典"""if not html:return Nonehtml_tree = etree.HTML(html)rows = html_tree.xpath('//*[text()="行政区划代码"]/../following-sibling::tr')location_dict = {}current_province = ''current_city = ''for row in rows:if not row.xpath('./td[2]/text()'):continuecode = row.xpath('./td[2]/text()')[0].strip()name = row.xpath('./td[3]/text()')[0].replace('*', '').strip()if code.endswith('0000'):  # 省级current_province = namelocation_dict[code] = {'province': current_province, 'city': '', 'district': ''}elif code.endswith('00'):  # 市级current_city = namelocation_dict[code] = {'province': current_province, 'city': current_city, 'district': ''}else:  # 区县级location_dict[code] = {'province': current_province, 'city': current_city, 'district': name}return location_dictdef generate_full_address(location_dict):"""生成完整的地址字符串"""finally_location_dict = {}for code, location in location_dict.items():full_address = f"{location['province']} {location['city']} {location['district']}".strip()finally_location_dict[code] = full_addressreturn finally_location_dictdef save_to_json(data, filename):"""将数据保存为JSON文件"""try:with open(filename, 'w+', encoding='utf-8') as f:json.dump(data, f, ensure_ascii=False, indent=2)print(f"数据已成功保存到 {filename}")except IOError as e:print(f"文件保存失败: {e}")def main():url = 'https://www.mca.gov.cn/mzsj/xzqh/2023/202301xzqh.html'html = fetch_html(url)if html:location_dict = parse_html(html)if location_dict:finally_location_dict = generate_full_address(location_dict)save_to_json(finally_location_dict, 'city_and_code.json')if __name__ == "__main__":main()# 提取编码对应省市区
# with open(r'city_and_code.json', "r", encoding="utf-8") as f:
#     dict_ = json.load(f)
# print(dict_["320507"])
http://www.dtcms.com/wzjs/509908.html

相关文章:

  • 做问卷网站个人如何在百度做广告
  • 重庆微信网站开网站注册时间查询
  • iis的默认网站没有自动启动电话营销外包公司
  • 木鱼网站建设游戏推广文案
  • 策划品牌全案衡阳seo快速排名
  • 郑州做网站建设的公司宣传网站有哪些
  • 国外网页设计评论网站谷歌官网
  • 人力资源外包灵活用工惠州seo收费
  • 网站推广 html关键词代码解说百度关键词排名代做
  • 什么是网站分析深圳网络推广哪家比较好
  • 企业做网站系统网站搜索引擎优化的步骤
  • 怎么选择丹徒网站建设如何做好网站推广优化
  • 湖南做网站 地址磐石网络制作一个网站的费用是多少
  • 网上商城网站建设解决方案电商平台链接怎么弄
  • 常州网站建设公司信息广州市口碑seo推广
  • 汇款账号 网站建设seo点击排名器
  • wordpress实现ajax沈阳seo排名公司
  • 成都网站建设技巧seo外链论坛
  • 文化网站建设需要的功能写文的免费软件
  • 网络工程师中级网站快速排名优化
  • 广州外贸网站建设公司快速排名seo
  • wordpress固定链接设置静态链接廊坊自动seo
  • 微信网站开发新开页面企业文化案例
  • 个人做企业网站seo推广工具
  • 浏阳市商务局网站溪江农贸市场建设在线crm网站建站
  • 网站托管服务使用于那种类型的网站网站制作过程
  • app制作开发公司怎么收费重庆专业seo
  • 广州设计公司排名榜网站优化包括哪些内容
  • 怎么用word做一个网站seo做的比较好的公司
  • 动漫一级a做爰片免费网站爱站网排名