lxml导入

通常的导入方式

from lxml import etree

python有自带的ElementTree库,但lxml在其基础上新增了特有的功能

如果代码仅使用ElementTree API,不依赖于lxml.etree特有的任何功能,那么还可使用(以下导入链的任何部分)作为回到原始ElementTree的方法

try:
  from lxml import etree
  print("running with lxml.etree")
except ImportError:
  try:
    # Python 2.5
    import xml.etree.cElementTree as etree
    print("running with cElementTree on Python 2.5+")
  except ImportError:
    try:
      # Python 2.5
      import xml.etree.ElementTree as etree
      print("running with ElementTree on Python 2.5+")
    except ImportError:
      try:
        # normal cElementTree install
        import cElementTree as etree
        print("running with cElementTree")
      except ImportError:
        try:
          # normal ElementTree install
          import elementtree.ElementTree as etree
          print("running with ElementTree")
        except ImportError:
          print("Failed to import ElementTree from any known place")
原文地址:https://www.cnblogs.com/shiliye/p/11855151.html