python读取xml格式数据
# -*- coding: UTF-8 -*- from xml.dom import minidom dom=minidom.parse(‘F:\\python_project\\爬虫\\双色球\\shuangseqiu.xml‘) root=dom.documentElement #打印节点信息 print(root.nodeName) #节点名称 print(root.nodeValue) #节点值 print(root.nodeType) #节点类型,如果是元素节点返回1,属性节点返回2 ‘‘‘ #读取文本节点的值 names=root.getElementsByTagName(‘name‘) ages=root.getElementsByTagName(‘age‘) citys=root.getElementsByTagName(‘city‘) print(names[0].firstChild.data) print(ages[0].firstChild.data) print(citys[0].firstChild.data) ‘‘‘ print(‘=============================‘) #打印xml子节点 tags=root.getElementsByTagName(‘Row‘) print(len(tags)) for item in tags: expect=item.getElementsByTagName(‘expect‘)[0].firstChild.data opencode=item.getElementsByTagName(‘opencode‘)[0].firstChild.data opentime=item.getElementsByTagName(‘opentime‘)[0].firstChild.data #print(type(expect)) print(expect,opencode,opentime) ‘‘‘ #获取属性节点的值 logins=root.getElementsByTagName(‘login‘) username=logins[0].getAttribute(‘username‘) print(username) password=logins[0].getAttribute(‘password‘) print(password) ‘‘‘ dow=minidom.parse(‘F:\\python_project\\爬虫\\大乐透\\daletou.xml‘) root=dow.documentElement print(‘=============================‘) rows=root.getElementsByTagName(‘row‘) print(len(rows)) for item in rows: expect=item.getAttribute(‘expect‘) opencode=item.getAttribute(‘opencode‘) opentime=item.getAttribute(‘opentime‘) print(expect,opencode,opentime)