`
sdmzhu3
  • 浏览: 30819 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

python 2个html 解析器的比较 lxml.html 和 libxml2dom

阅读更多
lxml.html 和 libxml2dom 都很强大
libxml2dom 和 lxml.html 都是基于 libxml的python包装,性能也都没有什么差别

lxml.html 使用起来比较方便,但是api 不标准
libxml2dom  使用标准的api 用起来会比较舒服 和javascript的api一样

#coding:utf-8


test_html="""
<html>
<body>
ddd
<p>
p1
</html>
<html>
  <body>  <p>p2
    fff<p>
    p3
    <i>iiii</i>
    xxx</p>
    <br>
    d<b>bb</b>
    </p>

"""
def test_lxml():
    import lxml.html

    doc=lxml.html.fromstring(test_html)
    #print dir(doc)
    #print doc.text
    for p in doc.xpath('//p'):
        #print '*', p.text
        #print dir(p )
        print 'p========',id(p)
        print lxml.html.tostring(p)

        print 'getchildren'
        for i in p.getchildren():
            print '#',i

        print 'itertext'
        for i in p.itertext():
            print '$',type(i),i
            
        print 'iter'
        for i in p.iter():
            print '$',type(i),i

        print 'items'
        for i in p.items():
            print '$',type(i),i

def test_xml2dom():
    import libxml2dom
    #help(libxml2dom)
    doc = libxml2dom.parseString(test_html, html=1)
    for p in doc.xpath('//p'):
        print 'p========='
        print p,type(p)
        #print dir(p)
        print p.childNodes
        for n in p.childNodes:
            #print dir(n)
            print n.textContent,n.nodeName,n.nodeType

def test_speed():
    import lxml.html
    import libxml2dom
    import time
    def load_lxml(html):

        doc=lxml.html.fromstring(html)
        return len(doc.xpath('//p'))
    def load_dom(html):
        doc=libxml2dom.parseString(html)
        return len(doc.xpath('//p'))

    def test_fun(f):
        t1=time.time()
        s=0
        for i in range(10000):
            s+=load_lxml(test_html)
        print f, time.time()-t1,s
    test_fun(load_lxml)
    test_fun(load_dom)
    test_fun(load_lxml)
    test_fun(load_dom)

if __name__=='__main__':
    test_xml2dom()
    test_lxml()
    test_speed()
    



主要测试下速度 和 比较不正规的html解析
都比较不错,性能也相差不大
执行结果
引用

p=========
<libxml2dom.Node object at 0x8f1cf2c> <class 'libxml2dom.Node'>
[<libxml2dom.Node object at 0x8f4a36c>]

p1
text 3
p=========
<libxml2dom.Node object at 0x8f1cf8c> <class 'libxml2dom.Node'>
[<libxml2dom.Node object at 0x8f4a38c>]
p2
    fff text 3
p=========
<libxml2dom.Node object at 0x8f4a32c> <class 'libxml2dom.Node'>
[<libxml2dom.Node object at 0x8f4a3ac>, <libxml2dom.Node object at 0x8f4a34c>, <libxml2dom.Node object at 0x8f4a3cc>]

    p3
     text 3
iiii i 1

    xxx text 3
p======== 150665036
<p>
p1
</p>
getchildren
itertext
$ <type 'str'>
p1

iter
$ <class 'lxml.html.HtmlElement'> <Element p at 8faf74c>
items
p======== 150666476
<p>p2
    fff</p>
getchildren
itertext
$ <type 'str'> p2
    fff
iter
$ <class 'lxml.html.HtmlElement'> <Element p at 8fafcec>
items
p======== 150666620
<p>
    p3
    <i>iiii</i>
    xxx</p>
   
getchildren
# <Element i at 8faf56c>
itertext
$ <type 'str'>
    p3
   
$ <type 'str'> iiii
$ <type 'str'>
    xxx
iter
$ <class 'lxml.html.HtmlElement'> <Element p at 8fafd7c>
$ <class 'lxml.html.HtmlElement'> <Element i at 90e6ecc>
items
<function load_lxml at 0x90f6e2c> 1.59452700615 30000
<function load_dom at 0x90f6e64> 1.68855881691 30000
<function load_lxml at 0x90f6e2c> 1.71267414093 30000
<function load_dom at 0x90f6e64> 1.74115109444 30000



以后还是倾向于使用 libxml2dom这个模块了
--------------
附上libxml2dom的api
------
Help on package libxml2dom:

NAME
    libxml2dom - DOM wrapper around libxml2, specifically the libxml2mod Python extension module.

FILE
    /usr/local/lib/python2.6/dist-packages/libxml2dom/__init__.py

DESCRIPTION
    Copyright (C) 2003, 2004, 2005, 2006, 2007 Paul Boddie <paul@boddie.org.uk>
   
    This program is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License as published by the Free
    Software Foundation; either version 3 of the License, or (at your option) any
    later version.
   
    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    details.
   
    You should have received a copy of the GNU Lesser General Public License along
    with this program.  If not, see <http://www.gnu.org/licenses/>.

PACKAGE CONTENTS
    events
    macrolib (package)
    soap
    svg
    xmlrpc
    xmpp

CLASSES
    __builtin__.list(__builtin__.object)
        NodeList
    __builtin__.object
        DocumentType
        Implementation
        NamedNodeMap
        NamedNodeMapIterator
        Node
            Attribute
            Document(_Document, Node)
    _Document
        Document(_Document, Node)
   
    class Attribute(Node)
     |  A class providing attribute access.
     | 
     |  Method resolution order:
     |      Attribute
     |      Node
     |      __builtin__.object
     | 
     |  Methods defined here:
     | 
     |  __init__(self, node, impl, ownerDocument=None, ownerElement=None)
     | 
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     | 
     |  parentNode
     | 
     |  ----------------------------------------------------------------------
     |  Methods inherited from Node:
     | 
     |  __eq__(self, other)
     | 
     |  __hash__(self)
     | 
     |  __ne__(self, other)
     | 
     |  appendChild(self, tmp)
     | 
     |  as_native_node(self)
     | 
     |  cloneNode(self, deep)
     | 
     |  createAttribute(self, name)
     | 
     |  createAttributeNS(self, ns, name)
     | 
     |  createCDATASection(self, value)
     | 
     |  createComment(self, value)
     | 
     |  createElement(self, name)
     | 
     |  createElementNS(self, ns, name)
     | 
     |  createTextNode(self, value)
     | 
     |  getAttribute(self, name)
     | 
     |  getAttributeNS(self, ns, localName)
     | 
     |  getAttributeNode(self, localName)
     | 
     |  getAttributeNodeNS(self, ns, localName)
     | 
     |  getElementById(self, identifier)
     | 
     |  getElementsByTagName(self, tagName)
     | 
     |  getElementsByTagNameNS(self, namespaceURI, localName)
     | 
     |  hasAttribute(self, name)
     | 
     |  hasAttributeNS(self, ns, localName)
     | 
     |  importNode(self, node, deep)
     | 
     |  insertBefore(self, tmp, oldNode)
     | 
     |  isSameNode(self, other)
     | 
     |  normalize(self)
     | 
     |  removeAttribute(self, name)
     | 
     |  removeAttributeNS(self, ns, localName)
     | 
     |  removeChild(self, tmp)
     | 
     |  replaceChild(self, tmp, oldNode)
     | 
     |  setAttribute(self, name, value)
     | 
     |  setAttributeNS(self, ns, name, value)
     | 
     |  setAttributeNode(self, node)
     | 
     |  setAttributeNodeNS(self, node)
     | 
     |  toFile(self, f, encoding=None, prettyprint=0)
     | 
     |  toStream(self, stream, encoding=None, prettyprint=0)
     | 
     |  toString(self, encoding=None, prettyprint=0)
     | 
     |  xpath(self, expr, variables=None, namespaces=None)
     | 
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from Node:
     | 
     |  __dict__
     |      dictionary for instance variables (if defined)
     | 
     |  __weakref__
     |      list of weak references to the object (if defined)
     | 
     |  attributes
     | 
     |  childNodes
     | 
     |  data
     | 
     |  doctype
     | 
     |  firstChild
     | 
     |  lastChild
     | 
     |  localName
     | 
     |  name
     | 
     |  namespaceURI
     | 
     |  nextSibling
     | 
     |  nodeName
     | 
     |  nodeType
     | 
     |  nodeValue
     | 
     |  prefix
     | 
     |  previousSibling
     | 
     |  publicId
     | 
     |  systemId
     | 
     |  tagName
     | 
     |  textContent
     | 
     |  value
     | 
     |  ----------------------------------------------------------------------
     |  Data and other attributes inherited from Node:
     | 
     |  ATTRIBUTE_NODE = 2
     | 
     |  COMMENT_NODE = 8
     | 
     |  DOCUMENT_NODE = 9
     | 
     |  DOCUMENT_TYPE_NODE = 10
     | 
     |  ELEMENT_NODE = 1
     | 
     |  ENTITY_NODE = 6
     | 
     |  ENTITY_REFERENCE_NODE = 5
     | 
     |  NOTATION_NODE = 12
     | 
     |  PROCESSING_INSTRUCTION_NODE = 7
     | 
     |  TEXT_NODE = 3
     | 
     |  entities = {}
     | 
     |  notations = {}
   
    class Document(_Document, Node)
     |  A generic document class. Specialised document classes should inherit from
     |  the _Document class and their own variation of Node.
     | 
     |  Method resolution order:
     |      Document
     |      _Document
     |      Node
     |      __builtin__.object
     | 
     |  Methods inherited from _Document:
     | 
     |  __del__(self)
     | 
     |  __init__(self, node, impl)
     | 
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from _Document:
     | 
     |  documentElement
     | 
     |  ownerDocument
     | 
     |  ----------------------------------------------------------------------
     |  Methods inherited from Node:
     | 
     |  __eq__(self, other)
     | 
     |  __hash__(self)
     | 
     |  __ne__(self, other)
     | 
     |  appendChild(self, tmp)
     | 
     |  as_native_node(self)
     | 
     |  cloneNode(self, deep)
     | 
     |  createAttribute(self, name)
     | 
     |  createAttributeNS(self, ns, name)
     | 
     |  createCDATASection(self, value)
     | 
     |  createComment(self, value)
     | 
     |  createElement(self, name)
     | 
     |  createElementNS(self, ns, name)
     | 
     |  createTextNode(self, value)
     | 
     |  getAttribute(self, name)
     | 
     |  getAttributeNS(self, ns, localName)
     | 
     |  getAttributeNode(self, localName)
     | 
     |  getAttributeNodeNS(self, ns, localName)
     | 
     |  getElementById(self, identifier)
     | 
     |  getElementsByTagName(self, tagName)
     | 
     |  getElementsByTagNameNS(self, namespaceURI, localName)
     | 
     |  hasAttribute(self, name)
     | 
     |  hasAttributeNS(self, ns, localName)
     | 
     |  importNode(self, node, deep)
     | 
     |  insertBefore(self, tmp, oldNode)
     | 
     |  isSameNode(self, other)
     | 
     |  normalize(self)
     | 
     |  removeAttribute(self, name)
     | 
     |  removeAttributeNS(self, ns, localName)
     | 
     |  removeChild(self, tmp)
     | 
     |  replaceChild(self, tmp, oldNode)
     | 
     |  setAttribute(self, name, value)
     | 
     |  setAttributeNS(self, ns, name, value)
     | 
     |  setAttributeNode(self, node)
     | 
     |  setAttributeNodeNS(self, node)
     | 
     |  toFile(self, f, encoding=None, prettyprint=0)
     | 
     |  toStream(self, stream, encoding=None, prettyprint=0)
     | 
     |  toString(self, encoding=None, prettyprint=0)
     | 
     |  xpath(self, expr, variables=None, namespaces=None)
     | 
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from Node:
     | 
     |  __dict__
     |      dictionary for instance variables (if defined)
     | 
     |  __weakref__
     |      list of weak references to the object (if defined)
     | 
     |  attributes
     | 
     |  childNodes
     | 
     |  data
     | 
     |  doctype
     | 
     |  firstChild
     | 
     |  lastChild
     | 
     |  localName
     | 
     |  name
     | 
     |  namespaceURI
     | 
     |  nextSibling
     | 
     |  nodeName
     | 
     |  nodeType
     | 
     |  nodeValue
     | 
     |  parentNode
     | 
     |  prefix
     | 
     |  previousSibling
     | 
     |  publicId
     | 
     |  systemId
     | 
     |  tagName
     | 
     |  textContent
     | 
     |  value
     | 
     |  ----------------------------------------------------------------------
     |  Data and other attributes inherited from Node:
     | 
     |  ATTRIBUTE_NODE = 2
     | 
     |  COMMENT_NODE = 8
     | 
     |  DOCUMENT_NODE = 9
     | 
     |  DOCUMENT_TYPE_NODE = 10
     | 
     |  ELEMENT_NODE = 1
     | 
     |  ENTITY_NODE = 6
     | 
     |  ENTITY_REFERENCE_NODE = 5
     | 
     |  NOTATION_NODE = 12
     | 
     |  PROCESSING_INSTRUCTION_NODE = 7
     | 
     |  TEXT_NODE = 3
     | 
     |  entities = {}
     | 
     |  notations = {}
   
    class DocumentType(__builtin__.object)
     |  A class providing a container for document type information.
     | 
     |  Methods defined here:
     | 
     |  __init__(self, localName, publicId, systemId)
     | 
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     | 
     |  __dict__
     |      dictionary for instance variables (if defined)
     | 
     |  __weakref__
     |      list of weak references to the object (if defined)
   
    class Implementation(__builtin__.object)
     |  Contains an abstraction over the DOM implementation.
     | 
     |  Methods defined here:
     | 
     |  adoptDocument(self, node)
     | 
     |  createDocument(self, namespaceURI, localName, doctype)
     | 
     |  createDocumentType(self, localName, publicId, systemId)
     | 
     |  get_node(self, _node, context_node)
     | 
     |  get_node_or_none(self, _node, context_node)
     | 
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     | 
     |  __dict__
     |      dictionary for instance variables (if defined)
     | 
     |  __weakref__
     |      list of weak references to the object (if defined)
   
    class NamedNodeMap(__builtin__.object)
     |  A wrapper around Node objects providing DOM and dictionary convenience
     |  methods.
     | 
     |  Methods defined here:
     | 
     |  __delitem__(self, name)
     | 
     |  __getitem__(self, name)
     | 
     |  __init__(self, node, impl)
     | 
     |  __iter__(self)
     | 
     |  __repr__(self)
     | 
     |  __setitem__(self, name, node)
     | 
     |  __str__(self)
     | 
     |  getNamedItem(self, name)
     | 
     |  getNamedItemNS(self, ns, localName)
     | 
     |  items(self)
     | 
     |  keys(self)
     | 
     |  removeNamedItem(self, name)
     | 
     |  removeNamedItemNS(self, ns, localName)
     | 
     |  setNamedItem(self, node)
     | 
     |  setNamedItemNS(self, node)
     | 
     |  values(self)
     | 
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     | 
     |  __dict__
     |      dictionary for instance variables (if defined)
     | 
     |  __weakref__
     |      list of weak references to the object (if defined)
     | 
     |  length
   
    class NamedNodeMapIterator(__builtin__.object)
     |  An iterator over a NamedNodeMap.
     | 
     |  Methods defined here:
     | 
     |  __init__(self, nodemap)
     | 
     |  next(self)
     | 
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     | 
     |  __dict__
     |      dictionary for instance variables (if defined)
     | 
     |  __weakref__
     |      list of weak references to the object (if defined)
   
    class Node(__builtin__.object)
     |  A DOM-style wrapper around libxml2mod objects.
     | 
     |  Methods defined here:
     | 
     |  __eq__(self, other)
     | 
     |  __hash__(self)
     | 
     |  __init__(self, node, impl=None, ownerDocument=None)
     | 
     |  __ne__(self, other)
     | 
     |  appendChild(self, tmp)
     | 
     |  as_native_node(self)
     | 
     |  cloneNode(self, deep)
     | 
     |  createAttribute(self, name)
     | 
     |  createAttributeNS(self, ns, name)
     | 
     |  createCDATASection(self, value)
     | 
     |  createComment(self, value)
     | 
     |  createElement(self, name)
     | 
     |  createElementNS(self, ns, name)
     | 
     |  createTextNode(self, value)
     | 
     |  getAttribute(self, name)
     | 
     |  getAttributeNS(self, ns, localName)
     | 
     |  getAttributeNode(self, localName)
     | 
     |  getAttributeNodeNS(self, ns, localName)
     | 
     |  getElementById(self, identifier)
     | 
     |  getElementsByTagName(self, tagName)
     | 
     |  getElementsByTagNameNS(self, namespaceURI, localName)
     | 
     |  hasAttribute(self, name)
     | 
     |  hasAttributeNS(self, ns, localName)
     | 
     |  importNode(self, node, deep)
     | 
     |  insertBefore(self, tmp, oldNode)
     | 
     |  isSameNode(self, other)
     | 
     |  normalize(self)
     | 
     |  removeAttribute(self, name)
     | 
     |  removeAttributeNS(self, ns, localName)
     | 
     |  removeChild(self, tmp)
     | 
     |  replaceChild(self, tmp, oldNode)
     | 
     |  setAttribute(self, name, value)
     | 
     |  setAttributeNS(self, ns, name, value)
     | 
     |  setAttributeNode(self, node)
     | 
     |  setAttributeNodeNS(self, node)
     | 
     |  toFile(self, f, encoding=None, prettyprint=0)
     | 
     |  toStream(self, stream, encoding=None, prettyprint=0)
     | 
     |  toString(self, encoding=None, prettyprint=0)
     | 
     |  xpath(self, expr, variables=None, namespaces=None)
     | 
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     | 
     |  __dict__
     |      dictionary for instance variables (if defined)
     | 
     |  __weakref__
     |      list of weak references to the object (if defined)
     | 
     |  attributes
     | 
     |  childNodes
     | 
     |  data
     | 
     |  doctype
     | 
     |  firstChild
     | 
     |  lastChild
     | 
     |  localName
     | 
     |  name
     | 
     |  namespaceURI
     | 
     |  nextSibling
     | 
     |  nodeName
     | 
     |  nodeType
     | 
     |  nodeValue
     | 
     |  parentNode
     | 
     |  prefix
     | 
     |  previousSibling
     | 
     |  publicId
     | 
     |  systemId
     | 
     |  tagName
     | 
     |  textContent
     | 
     |  value
     | 
     |  ----------------------------------------------------------------------
     |  Data and other attributes defined here:
     | 
     |  ATTRIBUTE_NODE = 2
     | 
     |  COMMENT_NODE = 8
     | 
     |  DOCUMENT_NODE = 9
     | 
     |  DOCUMENT_TYPE_NODE = 10
     | 
     |  ELEMENT_NODE = 1
     | 
     |  ENTITY_NODE = 6
     | 
     |  ENTITY_REFERENCE_NODE = 5
     | 
     |  NOTATION_NODE = 12
     | 
     |  PROCESSING_INSTRUCTION_NODE = 7
     | 
     |  TEXT_NODE = 3
     | 
     |  entities = {}
     | 
     |  notations = {}
   
    class NodeList(__builtin__.list)
     |  A wrapper around node lists.
     | 
     |  Method resolution order:
     |      NodeList
     |      __builtin__.list
     |      __builtin__.object
     | 
     |  Methods defined here:
     | 
     |  item(self, index)
     | 
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     | 
     |  __dict__
     |      dictionary for instance variables (if defined)
     | 
     |  __weakref__
     |      list of weak references to the object (if defined)
     | 
     |  length
     | 
     |  ----------------------------------------------------------------------
     |  Methods inherited from __builtin__.list:
     | 
     |  __add__(...)
     |      x.__add__(y) <==> x+y
     | 
     |  __contains__(...)
     |      x.__contains__(y) <==> y in x
     | 
     |  __delitem__(...)
     |      x.__delitem__(y) <==> del x[y]
     | 
     |  __delslice__(...)
     |      x.__delslice__(i, j) <==> del x[i:j]
     |     
     |      Use of negative indices is not supported.
     | 
     |  __eq__(...)
     |      x.__eq__(y) <==> x==y
     | 
     |  __ge__(...)
     |      x.__ge__(y) <==> x>=y
     | 
     |  __getattribute__(...)
     |      x.__getattribute__('name') <==> x.name
     | 
     |  __getitem__(...)
     |      x.__getitem__(y) <==> x[y]
     | 
     |  __getslice__(...)
     |      x.__getslice__(i, j) <==> x[i:j]
     |     
     |      Use of negative indices is not supported.
     | 
     |  __gt__(...)
     |      x.__gt__(y) <==> x>y
     | 
     |  __iadd__(...)
     |      x.__iadd__(y) <==> x+=y
     | 
     |  __imul__(...)
     |      x.__imul__(y) <==> x*=y
     | 
     |  __init__(...)
     |      x.__init__(...) initializes x; see x.__class__.__doc__ for signature
     | 
     |  __iter__(...)
     |      x.__iter__() <==> iter(x)
     | 
     |  __le__(...)
     |      x.__le__(y) <==> x<=y
     | 
     |  __len__(...)
     |      x.__len__() <==> len(x)
     | 
     |  __lt__(...)
     |      x.__lt__(y) <==> x<y
     | 
     |  __mul__(...)
     |      x.__mul__(n) <==> x*n
     | 
     |  __ne__(...)
     |      x.__ne__(y) <==> x!=y
     | 
     |  __repr__(...)
     |      x.__repr__() <==> repr(x)
     | 
     |  __reversed__(...)
     |      L.__reversed__() -- return a reverse iterator over the list
     | 
     |  __rmul__(...)
     |      x.__rmul__(n) <==> n*x
     | 
     |  __setitem__(...)
     |      x.__setitem__(i, y) <==> x[i]=y
     | 
     |  __setslice__(...)
     |      x.__setslice__(i, j, y) <==> x[i:j]=y
     |     
     |      Use  of negative indices is not supported.
     | 
     |  __sizeof__(...)
     |      L.__sizeof__() -- size of L in memory, in bytes
     | 
     |  append(...)
     |      L.append(object) -- append object to end
     | 
     |  count(...)
     |      L.count(value) -> integer -- return number of occurrences of value
     | 
     |  extend(...)
     |      L.extend(iterable) -- extend list by appending elements from the iterable
     | 
     |  index(...)
     |      L.index(value, [start, [stop]]) -> integer -- return first index of value.
     |      Raises ValueError if the value is not present.
     | 
     |  insert(...)
     |      L.insert(index, object) -- insert object before index
     | 
     |  pop(...)
     |      L.pop([index]) -> item -- remove and return item at index (default last).
     |      Raises IndexError if list is empty or index is out of range.
     | 
     |  remove(...)
     |      L.remove(value) -- remove first occurrence of value.
     |      Raises ValueError if the value is not present.
     | 
     |  reverse(...)
     |      L.reverse() -- reverse *IN PLACE*
     | 
     |  sort(...)
     |      L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
     |      cmp(x, y) -> -1, 0, 1
     | 
     |  ----------------------------------------------------------------------
     |  Data and other attributes inherited from __builtin__.list:
     | 
     |  __hash__ = None
     | 
     |  __new__ = <built-in method __new__ of type object at 0x822be40>
     |      T.__new__(S, ...) -> a new object with type S, a subtype of T

FUNCTIONS
    adoptNodes(nodes, impl=None)
        A special utility method which adopts the given low-level 'nodes' and which
        returns a list of high-level equivalents. This is currently experimental and
        should not be casually used.
   
    createDocument(namespaceURI, localName, doctype)
   
    createDocumentType(localName, publicId, systemId)
   
    getDOMImplementation()
        Return the default DOM implementation.
   
    parse(stream_or_string, html=0, htmlencoding=None, unfinished=0, impl=None)
        Parse the given 'stream_or_string', where the supplied object can either be
        a stream (such as a file or stream object), or a string (containing the
        filename of a document). The optional parameters described below should be
        provided as keyword arguments.
       
        If the optional 'html' parameter is set to a true value, the content to be
        parsed will be treated as being HTML rather than XML. If the optional
        'htmlencoding' is specified, HTML parsing will be performed with the
        document encoding assumed to that specified.
       
        If the optional 'unfinished' parameter is set to a true value, unfinished
        documents will be parsed, even though such documents may be missing content
        such as closing tags.
       
        A document object is returned by this function.
   
    parseFile(filename, html=0, htmlencoding=None, unfinished=0, impl=None)
        Parse the file having the given 'filename'. The optional parameters
        described below should be provided as keyword arguments.
       
        If the optional 'html' parameter is set to a true value, the content to be
        parsed will be treated as being HTML rather than XML. If the optional
        'htmlencoding' is specified, HTML parsing will be performed with the
        document encoding assumed to that specified.
       
        If the optional 'unfinished' parameter is set to a true value, unfinished
        documents will be parsed, even though such documents may be missing content
        such as closing tags.
       
        A document object is returned by this function.
   
    parseString(s, html=0, htmlencoding=None, unfinished=0, impl=None)
        Parse the content of the given string 's'. The optional parameters described
        below should be provided as keyword arguments.
       
        If the optional 'html' parameter is set to a true value, the content to be
        parsed will be treated as being HTML rather than XML. If the optional
        'htmlencoding' is specified, HTML parsing will be performed with the
        document encoding assumed to that specified.
       
        If the optional 'unfinished' parameter is set to a true value, unfinished
        documents will be parsed, even though such documents may be missing content
        such as closing tags.
       
        A document object is returned by this function.
   
    parseURI(uri, html=0, htmlencoding=None, unfinished=0, impl=None)
        Parse the content found at the given 'uri'. The optional parameters
        described below should be provided as keyword arguments.
       
        If the optional 'html' parameter is set to a true value, the content to be
        parsed will be treated as being HTML rather than XML. If the optional
        'htmlencoding' is specified, HTML parsing will be performed with the
        document encoding assumed to that specified.
       
        If the optional 'unfinished' parameter is set to a true value, unfinished
        documents will be parsed, even though such documents may be missing content
        such as closing tags.
       
        XML documents are retrieved using libxml2's own network capabilities; HTML
        documents are retrieved using the urllib module provided by Python. To
        retrieve either kind of document using Python's own modules for this purpose
        (such as urllib), open a stream and pass it to the parse function:
       
        f = urllib.urlopen(uri)
        try:
            doc = libxml2dom.parse(f, html)
        finally:
            f.close()
       
        A document object is returned by this function.
   
    toFile(node, filename, encoding=None, prettyprint=0)
        Write the serialised form of the given 'node' and its children to a file
        having the given 'filename'. The optional 'encoding' can be used to override
        the default character encoding used in the serialisation. The optional
        'prettyprint' indicates whether the serialised form is prettyprinted or not
        (the default setting).
   
    toStream(node, stream, encoding=None, prettyprint=0)
        Write the serialised form of the given 'node' and its children to the given
        'stream'. The optional 'encoding' can be used to override the default
        character encoding used in the serialisation. The optional 'prettyprint'
        indicates whether the serialised form is prettyprinted or not (the default
        setting).
   
    toString(node, encoding=None, prettyprint=0)
        Return a string containing the serialised form of the given 'node' and its
        children. The optional 'encoding' can be used to override the default
        character encoding used in the serialisation. The optional 'prettyprint'
        indicates whether the serialised form is prettyprinted or not (the default
        setting).

DATA
    HTML_PARSE_NOERROR = 32
    HTML_PARSE_NONET = 2048
    HTML_PARSE_NOWARNING = 64
    XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace'
    XML_PARSE_NOERROR = 32
    XML_PARSE_NONET = 2048
    XML_PARSE_NOWARNING = 64
    __version__ = '0.4.5'
    default_impl = <libxml2dom.Implementation object at 0xb748e0ec>
    default_ns = {'xml': 'http://www.w3.org/XML/1998/namespace'}
    label = 'pi'
    null_value_node_types = [9, 10, 1, 6, 5, 12]
    value = 7

VERSION
    0.4.5

分享到:
评论
1 楼 joyeye 2010-04-14  
谢谢分享经验
不过以后还是希望能把结论放到明显的地方。方便大家阅读。

相关推荐

    python2.7下lxml库下载安装以及代码示例

    1. **XML解析**:`lxml`提供多种解析XML文档的方式,如`ElementTree API`,`XMLPullParser`,以及基于SAX和DOM的解析器。例如,使用`ElementTree API`解析XML: ```python from lxml import etree xml_string =...

    Python库 | lxml-2.1.4.win32-py2.6.exe

    2. **使用**:在Python代码中,通过`import lxml`引入库,然后可以调用其提供的各种模块,如`lxml.etree`用于XML处理,`lxml.html`则专门用于HTML解析和操作。 **主要功能** 1. **XML解析**:`lxml.etree`模块提供...

    Python库 | lxml-3.8.0.win32-py3.2.exe

    它结合了Cython(Python到C的编译器)和两个C库——libxml2和libxslt,实现了对XML和HTML的高速解析和转换。这些特性使得lxml在处理大量数据时表现优秀,速度远超纯Python实现的库。 lxml库的核心功能包括: 1. **...

    lxml-4.5.0.tgz

    lxml是一个高效且功能强大的Python XML和HTML处理库,它结合了Cython编译的libxml2和libxslt库,提供了快速的解析、生成以及操作XML和HTML文档的能力。本文将深入探讨lxml库的基本概念、主要功能、安装方法以及在...

    LIBXML2相关文档

    LIBXML2是一个开源的XML解析库,用于处理XML(可扩展标记语言)和其他相关的格式,如HTML、XHTML和SVG。这个库由Gnome项目维护,广泛应用于各种操作系统,包括Linux、Unix、Windows等。LIBXML2提供了丰富的API(应用...

    libxml2-2.9.0.tar.gz

    libxml2库是C语言实现的XML解析器,它的设计目标是提供高效、稳定且功能全面的XML处理工具。这个库不仅支持XML解析,还包括了XPath(用于在XML文档中查找信息的表达式语言)、XInclude(用于在XML文档中合并内容的...

    lxml-4.1.1.tar.gz

    lxml-4.1.1是该库的一个特定版本,包含了对XML和HTML解析、XPath和XSLT的支持,以及DOM(Document Object Model)和CSS选择器。 在Windows环境下安装lxml-4.1.1,通常需要解决一些依赖问题,因为libxml2和libxslt是...

    用Python解析XML文件的软件实现.zip

    `lxml`是一个第三方库,它结合了`ElementTree`和`libxml2/libxslt`库,提供了更强大的功能和更好的性能。它支持XPath、CSS选择器以及XML Schema验证等高级特性。安装`lxml`后,可以这样使用: ```python from ...

    PyPI 官网下载 | lxml-2.2.5.tar.gz

    1. **XML 和 HTML 解析**:`lxml` 提供了多种解析器,支持 XML、HTML 以及 SAX 和 DOM 风格的事件驱动解析。它能够快速有效地处理大型文档,并且对 HTML 不规范的情况有很好的容错能力。 2. **XPath 和 XSLT 支持**...

    libxml2 源码

    此外,许多编程语言的XML处理库,如Python的lxml,也是基于libxml2实现的。 综上所述,libxml2是一个强大而灵活的XML处理工具,它的功能丰富,性能优秀,且有良好的社区支持,对于处理XML相关任务,libxml2是开发者...

    Python库 | lxml-4.6.0-cp35-cp35m-manylinux1_i686.whl

    `lxml`是Python中一个非常强大的XML和HTML处理库,它结合了C语言的速度和Python的易用性,为开发者提供了高效且灵活的数据解析、处理以及生成的能力。这个库源自libxml2和libxslt,因此在性能上表现优秀,尤其在处理...

    lxml-4.6.3-cp39-cp39-win_amd64.zip

    lxml是一个强大的Python XML处理库,它结合了Cython和libxml2/libxslt库,提供了高性能的XML和HTML处理能力。 描述中的 "python -lxml 4.6.3" 暗示我们正在讨论的是一个与Python环境相关的命令,用于安装或升级lxml...

    libxml2-2.9.0.zip

    1. **解析过程**:libxml2提供两种解析模式——SAX(Simple API for XML)和DOM(Document Object Model)。SAX是事件驱动的,逐行解析XML,适用于大型文档;DOM一次性加载整个文档到内存,适合小型文档或需要随机...

    2018最新版本lxml-4.2.5-cp37-cp37m-win_amd64

    lxml是Python中一个强大的XML处理库,它结合了C语言的libxml2和libxslt库,提供了极高的性能和稳定性。2018年发布的4.2.5版本是该库的一个重要更新,旨在提供最新的功能和优化。 **lxml的主要特性** 1. **高速解析...

    在windows系统中实现python3安装lxml

    lxml库是基于libxml2和libxslt库的Python接口,它结合了这些C库的速度和全面性,同时也保持了Pythonic的API,使得在Python中操作XML变得更为简便。然而,由于lxml不是Python的标准库,因此需要额外安装。在Windows和...

    PyPI 官网下载 | lxml-4.6.1-cp36-cp36m-macosx_10_9_x86_64.whl

    1. **XML和HTML解析**: `lxml`库基于libxml2和libxslt库,提供了快速而强大的XML和HTML解析功能。它支持SAX、DOM和lxml对象模型等多种解析方式,可以方便地处理复杂结构的文档。 2. **XPath和CSS选择器**: `lxml`...

    python教程-04-获取和设置标签内容(innerHTML).ev4.rar

    3. **lxml库**:lxml是一个更加强大的库,结合了C语言的libxml2和libxslt库,性能优异。它也支持innerHTML的操作。 ```python from lxml import etree # 解析HTML字符串或文件 root = etree.fromstring...

    pythonxml库

    2. **lxml库**: lxml是Python的一个第三方库,它结合了Cython编译的C库(如libxml2和libxslt),因此在性能上比标准库的ElementTree更胜一筹。lxml提供了与ElementTree类似的API,但增加了更多功能,如XPath和CSS...

    Python网页解析器使用实例详解

    在这个领域,Python提供了多种解析器供选择,包括正则表达式、内置的`html.parser`模块,以及第三方库如BeautifulSoup和lxml。 1. **正则表达式(re)**: - 正则表达式是基于字符串的模糊匹配技术,适用于简单的...

Global site tag (gtag.js) - Google Analytics