BeautifulSoup获取xml多层标签的内容

复制代码
file_name = r'C:/Users/HZJT0040/Desktop/Daliymed/410/2fbaadf6-c86c-48bb-bbba-803377841733.xml'

with open(file_name, 'r', encoding='utf-8') as f:                   # 读取xml文本
  html = f.read()
  pattern = re.compile('<structuredBody>(.*?)</structuredBody>',re.S)   # 正则匹配指定的标签内容
  page_data = re.findall(pattern, html)[0]
  try:
    soup = BeautifulSoup(page_data, 'lxml')
    result = soup.component.find_next_siblings()                  # find_next_siblings () 方法返回所有符合条件的后面的兄弟节点
    print(len(result))
    for i,ul in enumerate(result[1:]):                 # 获取外层标签指定的内容text 
      result = ul.text.replace("\t", "").replace("\xa0", "").strip() # 
      resultList = result.split('\n')
      for del_str in resultList:
        if del_str:
          print(del_str.replace("\n", ""))
  except Exception as e:
    print(e)
复制代码

 

posted @   林暗惊风  阅读(201)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示