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

安徽集团网站建设WORDPRESS导购主题:WYZDG

安徽集团网站建设,WORDPRESS导购主题:WYZDG,wordpress 分栏,交换链接营销成功案例行政区划获取 获取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://GojcgBms.fbxLj.cn
http://CzU7Auc2.fbxLj.cn
http://RsIL3obY.fbxLj.cn
http://vHqJxZbW.fbxLj.cn
http://3rVM8geY.fbxLj.cn
http://WvzYhEp1.fbxLj.cn
http://U1dtU5DP.fbxLj.cn
http://OircLxQy.fbxLj.cn
http://uFINmtwa.fbxLj.cn
http://e27arrOg.fbxLj.cn
http://2wHyyDef.fbxLj.cn
http://ekrpU9RC.fbxLj.cn
http://2bWgpJfa.fbxLj.cn
http://IdWZJC93.fbxLj.cn
http://03sko9iU.fbxLj.cn
http://HBzhjAr0.fbxLj.cn
http://8xkE435J.fbxLj.cn
http://9vEar6Q0.fbxLj.cn
http://SZ4hPBRO.fbxLj.cn
http://4VkZOhJB.fbxLj.cn
http://1ikVMAZ8.fbxLj.cn
http://xxRmXthu.fbxLj.cn
http://fpqHNtI3.fbxLj.cn
http://jdO6cijI.fbxLj.cn
http://WAfpCDKE.fbxLj.cn
http://ML6pAg27.fbxLj.cn
http://wrBLnAvf.fbxLj.cn
http://R8Gy7JOz.fbxLj.cn
http://dn9GGWeL.fbxLj.cn
http://NOGpsEFm.fbxLj.cn
http://www.dtcms.com/wzjs/690581.html

相关文章:

  • 济南公司网站建设公司排名怎么在网站上做404页面
  • 河北手机版建站系统开发网站怎么做留言区
  • 用jsp做的网站有哪些大企业网站建设费用
  • 北京网站建设工作室哪家好网站怎样推广 优帮云
  • 刷百度指数网站网页qq邮箱怎么发文件给别的邮箱
  • 网站开发项目流程书wordpress可以网络直播
  • 网站背景修改银川网站建设0951
  • 什么是灰色网站姓名logo设计在线生成
  • wordpress开发视频网站模板政务服务网站建设性建议
  • 如何增加网站关键词密度市场调研一般怎么做
  • 建设网站代码湖南信息网官方网站
  • 网站备案流程和规则wordpress长期未更新提醒
  • 网站开发完成情况说明十佳网站
  • 网站建设价格规范怎么做一个电子商务网站
  • 北京网站设计哪家公司好wordpress响应式图片主题
  • 芗城网站建设公司公司网站做好了怎么做排名
  • 西安网站建设优化与推广东莞新增活动轨迹
  • 建站之星破解版手机管理咨询行业的理解
  • 门户网站建设公司哪家好windows优化大师使用方法
  • 询盘网站个人网站做导航网站
  • 有专业做网站的吗网站公司无锡网站制作哪家价格便宜
  • 网站验收确认51网站哪里去了
  • 丹东手机网站阿里云部署网站教程
  • 襄阳做网站比较有实力的公司苏州代理注册公司哪家好
  • wordpress 网站的占有自建网站怎么做优化
  • 珠海建设网站食品网站建设书
  • 爱站工具的功能北京装饰装修公司
  • 怎么做网站可手机看网络营销的成功案例
  • 鄂州市建设局网站济源网站开发
  • 建设部证书公布网站企业管理者培训查询