xml 解析 python
1 综述
有很多种解析方法。
(1) DOM 缺点是:1 不能解析格式不正确或者不规则xml 2据说只能解析utf-8格式,非utf-8需要转码
与SAX比较,DOM典型的缺点是比较慢,消耗更多的内存,因为DOM会将整个XML数读入内存中,并为树
中的第一个节点建立一个对象。使用DOM的好处是你不需要对状态进行追踪,因为每一个节点都知道谁是它的
父节点,谁是子节点。但是DOM用起来有些麻烦。
Python中使用minidom解析xml,会遇到编码问题。minidom支持UTF-8的编码,对于其他的编码,必须进行转换,否则解析时会产生异常。下面以读取gb2312编码的xml文件为例。
sourceFile = codecs.open(sourceFilePath,mode='r')
xmlContentStr = sourceFile.read()
xmlContentStr = xmlContentStr.decode('gb2312').encode('utf-8')
xmlContentStr =xmlContentStr.replace('encoding="gb2312"','encoding="utf-8"')
sourceXML = minidom.parseString(xmlContentStr)
(2) beatifulsoup 特点是可以处理格式不正确的html文档
(3).SAX
(simple API for XML )
pyhton 标准库包含SAX解析器,SAX是一种典型的极为快速的工具,在解析XML时,不会占用大量内存。
但是这是基于回调机制的,因此在某些数据中,它会调用某些方法进行传递。这意味着必须为数据指定句柄,
以维持自己的状态,这是非常困难的。
(4) ElementTree(元素树)
ElementTree就像一个轻量级的DOM,具有方便友好的API。代码可用性好,速度快,消耗内存少,这里主要
介绍ElementTree。
2 DOM(Document
Object Model)
与SAX比较,DOM典型的缺点是比较慢,消耗更多的内存,因为DOM会将整个XML数读入内存中,并为树
中的第一个节点建立一个对象。使用DOM的好处是你不需要对状态进行追踪,因为每一个节点都知道谁是它的
父节点,谁是子节点。但是DOM用起来有些麻烦。
一 、xml.dom 解析XML的API描述
minidom.parse(filename)
加载读取XML文件
doc.documentElement
获取XML文档对象
node.getAttribute(AttributeName)
获取XML节点属性值
node.getElementsByTagName(TagName)
获取XML节点对象集合
node.childNodes #返回子节点列表。
node.childNodes[index].nodeValue
获取XML节点值
node.firstChild
#访问第一个节点。等价于pagexml.childNodes[0]
doc = minidom.parse(filename)
doc.toxml('UTF-8')
返回Node节点的xml表示的文本
Node.attributes["id"]
a.name #就是上面的 "id"
a.value #属性的值
访问元素属性
二、代码演示
1、创建user.xml文件,添加XMl节点

<?xml version="1.0" encoding="UTF-8" ?>
<users>
<user id="1000001">
<username>Admin</username>
<email>admin@live.cn</email>
<age>23</age>
<sex>男</sex>
</user>
<user id="1000002">
<username>Admin2</username>
<email>admin2@live.cn</email>
<age>22</age>
<sex>男</sex>
</user>
<user id="1000003">
<username>Admin3</username>
<email>admin3@live.cn</email>
<age>27</age>
<sex>男</sex>
</user>
<user id="1000004">
<username>Admin4</username>
<email>admin4@live.cn</email>
<age>25</age>
<sex>女</sex>
</user>
<user id="1000005">
<username>Admin5</username>
<email>admin5@live.cn</email>
<age>20</age>
<sex>男</sex>
</user>
<user id="1000006">
<username>Admin6</username>
<email>admin6@live.cn</email>
<age>23</age>
<sex>女</sex>
</user>
</users>

2、Demo.py解析user.xml文档数据

# -*- coding:utf-8 -*-
"""
* User: lhj588
* Date: 11-11-9
* Time: 13:20
* Desc:
"""
from xml.dom import minidom
def get_attrvalue(node, attrname):
return node.getAttribute(attrname) if node else ''
def get_nodevalue(node, index = 0):
return node.childNodes[index].nodeValue if node else ''
def get_xmlnode(node,name):
return node.getElementsByTagName(name) if node else []
def xml_to_string(filename='user.xml'):
doc = minidom.parse(filename)
return doc.toxml('UTF-8')
def get_xml_data(filename='user.xml'):
doc = minidom.parse(filename)
root = doc.documentElement
user_nodes = get_xmlnode(root,'user')
user_list=[]
for node in user_nodes:
user_id = get_attrvalue(node,'id')
node_name = get_xmlnode(node,'username')
node_email = get_xmlnode(node,'email')
node_age = get_xmlnode(node,'age')
node_sex = get_xmlnode(node,'sex')
user_name =get_nodevalue(node_name[0]).encode('utf-8','ignore')
user_email = get_nodevalue(node_email[0]).encode('utf-8','ignore')
user_age = int(get_nodevalue(node_age[0]))
user_sex = get_nodevalue(node_sex[0]).encode('utf-8','ignore')
user = {}
user['id'] , user['username'] , user['email'] , user['age'] , user['sex'] = (
int(user_id), user_name , user_email , user_age , user_sex
)
user_list.append(user)
return user_list
def test_xmltostring():
print xml_to_string()
def test_laod_xml():
user_list = get_xml_data()
for user in user_list :
#print user['sex']
print '-----------------------------------------------------'
if user:
user_str='编 号:%d\n用户名:%s\n性 别:%s\n年 龄:%s\n邮 箱:%s\n ' % (int(user['id']) , user['username'], user['sex'] , user['age'] , user['email'])
print user_str
print '====================================================='
if __name__ == "__main__":
test_xmltostring()
test_laod_xml()

3、测试效果
A、测试toxml
demo.py 文件中修改成
if __name__ == "__main__":
test_xmltostring()
执行打印结果:

<user id="1000001">
<username>Admin</username>
<email>admin@live.cn</email>
<age>23</age>
<sex>男</sex>
</user>
<user id="1000002">
<username>Admin2</username>
<email>admin2@live.cn</email>
<age>22</age>
<sex>男</sex>
</user>
<user id="1000003">
<username>Admin3</username>
<email>admin3@live.cn</email>
<age>27</age>
<sex>男</sex>
</user>
<user id="1000004">
<username>Admin4</username>
<email>admin4@live.cn</email>
<age>25</age>
<sex>女</sex>
</user>
<user id="1000005">
<username>Admin5</username>
<email>admin5@live.cn</email>
<age>20</age>
<sex>男</sex>
</user>
<user id="1000006">
<username>Admin6</username>
<email>admin6@live.cn</email>
<age>23</age>
<sex>女</sex>
</user>

B、测试解析XML
demo.py 文件中修改成
if __name__ == "__main__":
test_laod_xml()
执行打印出结果:

-----------------------------------------------------
编 号:1000001
用户名:Admin
性 别:男
年 龄:23
邮 箱:admin@live.cn
=====================================================
-----------------------------------------------------
编 号:1000002
用户名:Admin2
性 别:男
年 龄:22
邮 箱:admin2@live.cn
=====================================================
-----------------------------------------------------
编 号:1000003
用户名:Admin3
性 别:男
年 龄:27
邮 箱:admin3@live.cn
=====================================================
-----------------------------------------------------
编 号:1000004
用户名:Admin4
性 别:女
年 龄:25
邮 箱:admin4@live.cn
=====================================================
-----------------------------------------------------
编 号:1000005
用户名:Admin5
性 别:男
年 龄:20
邮 箱:admin5@live.cn
=====================================================
-----------------------------------------------------
编 号:1000006
用户名:Admin6
性 别:女
年 龄:23
邮 箱:admin6@live.cn
=====================================================
转自:http://www.cnblogs.com/lhj588/archive/2011/11/09/2242483.html
3 Beautiful
Soup
Beautiful
Soup 是用 Python 写的一个 HTML/XML 的解析器,它可以很好的处理不规范标记并生成剖析树。通常用来分析爬虫抓取的web文档。对于 不规则的 Html文档,也有很多的补全功能,节省了开发者的时间和精力。
Beautiful Soup 的官方文档齐全,将官方给出的例子实践一遍就能掌握。官方英文文档,中文文档
一 安装 Beautiful Soup
安装 BeautifulSoup 很简单,下载 BeautifulSoup 源码。解压运行
python setup.py install 即可。
测试安装是否成功。键入 import BeautifulSoup 如果没有异常,即成功安装
二 使用 BeautifulSoup
1. 导入BeautifulSoup ,创建BeautifulSoup 对象
|
1
2
3
4
5
6
7
8
9
10
11
12
|
fromBeautifulSoupimportBeautifulSoup #fromBeautifulSoupimportBeautifulStoneSoup #importBeautifulSoup # doc=[ '<html><head><title>Page, '<body><p, '<p, '</html>']#soup=BeautifulSoup(''.join(doc)) |
2. BeautifulSoup对象简介
用BeautifulSoup 解析 html文档时,BeautifulSoup将 html文档类似 dom文档树一样处理。BeautifulSoup文档树有三种基本对象。
2.1. soup BeautifulSoup.BeautifulSoup
|
1
2
|
type(soup)<class'BeautifulSoup.BeautifulSoup'> |
2.2. 标记 BeautifulSoup.Tag
|
1
2
|
type(soup.html)<class'BeautifulSoup.Tag'> |
2.3 文本 BeautifulSoup.NavigableString
|
1
2
|
type(soup.title.string)<class'BeautifulSoup.NavigableString'> |
3. BeautifulSoup 剖析树
3.1 BeautifulSoup.Tag对象方法
获取 标记对象(Tag)
标记名获取法 ,直接用 soup对象加标记名,返回 tag对象.这种方式,选取唯一标签的时候比较有用。或者根据树的结构去选取,一层层的选择
|
1
2
3
4
5
6
7
|
>>>>>><html><head><title>Pagetitle></head><body><pid="firstpara"align="center">Thisb>one</b>.</p><pid="secondpara"align="blah">Thisb>two</b>.</p></body></html>>>><class'BeautifulSoup.Tag'>>>><title>Pagetitle> |
content方法
content方法 根据文档树进行搜索,返回标记对象(tag)的列表
|
1
2
|
>>>[<html><head><title>Page/title></head><body><pid="firstpara"align="center">Thisisparagraph<b>one</b>.</p><pid="secondpara"align="blah">Thisisparagraph<b>two</b>.</p></body></html>] |
|
1
2
3
4
5
6
|
>>>0].contents[<head><title>Page/title></head>,id="firstpara"align="center">Thisisparagraph<b>one</b>.</p><pid="secondpara"align="blah">Thisisparagraph<b>two</b>.</p></body>]>>>len(soup.contents[0].contents)2>>>type(soup.contents[0].contents[1])<class'BeautifulSoup.Tag'> |
使用contents向后遍历树,使用parent向前遍历树
next 方法
获取树的子代元素,包括 Tag 对象 和 NavigableString 对象。。。
|
1
2
3
4
|
>>>next<title>Page/title>>>>next.nextu'Page |
|
1
2
3
4
5
|
>>>=soup.p>>><pid="firstpara"align="center">Thisisparagraph<b>one</b>.</p>>>>nextu'This |
nextSibling 下一个兄弟对象 包括 Tag 对象 和 NavigableString 对象
|
1
2
3
4
|
>>><body><pid="firstpara"align="center">Thisisparagraph<b>one</b>.</p><pid="secondpara"align="blah">Thisisparagraph<b>two</b>.</p></body>>>>next.nextSibling<b>one</b> |
与 nextSibling 相似的是 previousSibling,即上一个兄弟节点。
replacewith方法
将对象替换为,接受字符串参数
|
1
2
3
4
5
6
7
8
9
10
11
12
|
>>>=soup.head>>><head><title>Page/title></head>>>><html><head><title>Page/title></head><body><pid="firstpara"align="center">Thisisparagraph<b>one</b>.</p><pid="secondpara"align="blah">Thisisparagraph<b>two</b>.</p></body></html>>>>'head)>>><head><title>Page/title></head>>>>>>><html>headid="firstpara"align="center">Thisisparagraph<b>one</b>.</p><pid="secondpara"align="blah">Thisisparagraph<b>two</b>.</p></body></html>>>> |
搜索方法
搜索提供了两个方法,一个是 find,一个是findAll。这里的两个方法(findAll和 find)仅对Tag对象以及,顶层剖析对象有效,但 NavigableString不可用。
findAll(name, attrs, recursive, text, limit, **kwargs)
接受一个参数,标记名
寻找文档所有 P标记,返回一个列表
|
1
2
3
4
|
>>>'p')[<pid="firstpara"align="center">Thisisparagraph<b>one</b>.</p>,id="secondpara"align="blah">Thisisparagraph<b>two</b>.</p>]>>>type(soup.findAll('p'))<type'list'> |
寻找 id="secondpara"的 p 标记,返回一个结果集
|
1
2
3
|
>>>=type(soup.findAll('p',id='firstpara'))>>><class'BeautifulSoup.ResultSet'> |
传一个属性或多个属性对
|
1
2
3
4
5
|
>>>=soup.findAll('p',{'align':'blah'})>>>[<pid="secondpara"align="blah">Thisisparagraph<b>two</b>.</p>]>>>type(p2)<class'BeautifulSoup.ResultSet'> |
利用正则表达式
|
1
2
|
>>>id=re.compile("para$"))[<pid="firstpara"align="center">Thisisparagraph<b>one</b>.</p>,id="secondpara"align="blah">Thisisparagraph<b>two</b>.</p>] |
读取和修改属性
|
1
2
3
4
5
6
7
8
9
10
11
12
|
>>>=soup.p>>><pid="firstpara"align="center">Thisisparagraph<b>one</b>.</p>>>>'id']u'firstpara'>>>'id']='changeid'>>><pid="changeid"align="center">Thisisparagraph<b>one</b>.</p>>>>'class']='new>>><pid="changeid"align="center"class="new>Thisisparagraph<b>one</b>.</p>>>> |
剖析树基本方法就这些,还有其他一些,以及如何配合正则表达式。具体请看官方文档
3.2 BeautifulSoup.NavigableString对象方法
NavigableString 对象方法比较简单,获取其内容
|
1
2
3
4
5
6
7
8
9
|
>>><title>Page/title>>>>=soup.title.next>>>u'Page>>>type(title)<class'BeautifulSoup.NavigableString'>>>>u'Page |
至于如何遍历树,进而分析文档,已经 XML 文档的分析方法,可以参考官方文档。
转自:http://rsj217.diandian.com/post/2012-11-01/40041235132
xml 解析 python的更多相关文章
- Python 爬虫笔记、多线程、xml解析、基础笔记(不定时更新)
1 Python学习网址:http://www.runoob.com/python/python-multithreading.html
- Python XML解析(转载)
Python XML解析 什么是XML? XML 指可扩展标记语言(eXtensible Markup Language). 你可以通过本站学习XML教程 XML 被设计用来传输和存储数据. XML是 ...
- Xml解析之——Java/Android/Python
Xml解析之——Java/Android/Python 一.Xml文件 test.xml <note> <to>George</to> <from>Jo ...
- Python XML解析之DOM
DOM说明: DOM:Document Object Model API DOM是一种跨语言的XML解析机制,DOM把整个XML文件或字符串在内存中解析为树型结构方便访问. https://docs. ...
- Python XML解析之ElementTree
参考网址: http://www.runoob.com/python/python-xml.html https://docs.python.org/2/library/xml.etree.eleme ...
- python大法好——Python XML解析
Python XML解析 什么是XML? XML 被设计用来传输和存储数据. XML是一套定义语义标记的规则,这些标记将文档分成许多部件并对这些部件加以标识. 它也是元标记语言,即定义了用于定义其他与 ...
- python使用sax实现xml解析
之前在使用xml解析的时候,在网上搜了很多教程,最终没有能按照网上的教程实现需求. 所以呢,只好自己去看源码,在sax的__init__.py下看到这么一段代码: 1 def parse(source ...
- 【Python】xml 解析
1. XML:指可扩展标记语言,是一种标记语言,用于存储数据和传输数据,但没有像HTML那样具有预定义标签,需要程序猿自定义标签 2. XML的解析:读取XML数据结构中的某些信息,比如读取书的属性 ...
- Python之XML解析详解
什么是XML? XML 指可扩展标记语言(eXtensible Markup Language). XML 被设计用来传输和存储数据. XML是一套定义语义标记的规则,这些标记将文档分成许多部件并对这 ...
随机推荐
- diff and patch
A patch captures the changes of two different files (oldfile and newfile). Given the oldfile and the ...
- wamp本地能访问,但局域网不能访问的解决办法
Apache的配置文件.
- 贪心+树状数组维护一下 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D
http://codeforces.com/contest/724/problem/D 题目大意:给你一个串,从串中挑选字符,挑选是有条件的,按照这个条件所挑选出来的字符集合sort一定是最后选择当中 ...
- postfix防垃圾邮件
Postfix 2.x 打开/etc/postfix/main.cf文件,在其中增加如下的几行(如果相关的配置存在,就替换之): vi /etc/postfix/main.cf [...] smtpd ...
- Docker 简介及安装
Docker简介: 什么是Docker?将应用程序自动部署到容器 go语言开源引擎 Github地址:https://github.com/docker/docker 2013年初 dotCloud ...
- iOS中的多线程基础
NSThread NSThread是一个苹果封装过的,面向对象的线程对象.但是它的生命周期需要我们自己来手动管理,所以使用不是很常见,比如[NSThread currentThread],它可以获取当 ...
- 2.1 sikuli 中编程运行
1.用sikuli编程时,多用wait()语句,因为很多时候没有给它一定的识别时间,就容易出错. 比如下图,保证页面加载时间 1.Sikuli中 ,可以加# 进行注释 但是注释有的时候也会不起作用,比 ...
- POJ 2975 Nim 尼姆博弈
题目大意:尼姆博弈,求先手必胜的情况数 题目思路:判断 ans=(a[1]^a[2]--^a[n]),求ans^a[i] < a[i]的个数. #include<iostream> ...
- C: define many functions using predefine..
/* Defines COUNTER. There must be exactly one such definition at file scope * within a program. */ # ...
- POJ3264/RMQ
题目链接 /* 询问一段区间内的元素差值最大是多少,用RMQ维护一个最大值和一个最小值,相减即可. */ #include<cstdio> #include<cstring> ...