What are the differences between lxml and ElementTree?

前端 未结 2 1694
不思量自难忘°
不思量自难忘° 2020-12-14 05:53

When it comes to generating XML data in Python, there are two libraries I often see recommended: lxml and ElementTree

2条回答
  •  时光取名叫无心
    2020-12-14 06:38

    ElementTree comes built-in with the Python standard library which includes other data modules types such as json and csv. This means the module ships with each installation of Python. For most normal XML operations including building document trees and simple searching and parsing of element attributes and node values, even namespaces, ElementTree is a reliable handler.

    Lxml is a third-party module that requires installation. In many ways lxml actually extends ElementTree as most operations in the built-in module are available. Chief among this extension is that lxml supports both XPath 1.0 and XSLT 1.0. Additionally, lxml can parse HTML documents that are not XML compliant and hence is used for web-scraping operations and even as the parser in BeautifulSoup and engine in Pandas, pandas.read_html(). Other useful, common features of lxml include pretty_print output, objectify, and sax support. Of course too as a third-party module, versions with additional features are readily accessible compared to the standard library.

提交回复
热议问题