Lxm is a python library for HTML/XML parsing and Dom creation. lxml features powerful functions and good performance. xml contains elementtree
, Html5lib
, Beautfulsoup
But lxml also has its own library. Therefore, lxml is complicated and it is difficult for users to understand its relationship for the first time.
Install lxml
Lxml installation dependency
Python-devel, libxml2-devel, libxslt-devel,
After installation, download
Http://codespeak.net/lxml/lxml-2.2.8.tgz,
Tar zxvf lxml-2.2.8.tgz,
Then Python setup. py install.
1. parse HTML and create dom
>>> Import lxml. etree as etree
>>> Html = '<HTML> <body id = "1"> ABC <div> 123 </div> def <div> 456 </div> Ghi </body> >>> Dom = etree. fromstring (HTML)
>>> Etree. tostring (DOM)
'<HTML> <body id = "1"> ABC <div> 123 </div> def <div> 456 </div> Ghi </body>
If the beautifulsoup parser is used
>>> Import lxml.html. soupparser as soupparser
>>> Dom = soupparser. fromstring (HTML)
>>> Etree. tostring (DOM)
'<HTML> <body id = "1"> ABC <div> 123 </div> def <div> 456 </div> Ghi </body>
However, I strongly recommend using soupparser because it has much stronger processing capability than etree.
2. Access element by Dom
Child element length
>>> Len (DOM)
1
Access child elements:
>>> Dom [0]. Tag
'Body'
Loop access:
>>> For child in DOM:
... Print child. Tag
...
Body
View node Indexes
>>> Body = Dom [0]
>>> Dom. Index (Body)
0
Obtain the parent node by byte
>>> Body. getparent (). Tag
'Html'
Access all subnodes
>>> For ele in Dom. ITER ():
... Print ELE. Tag
...
Html
Body
Div
Div
3. Access Node attributes
>>> Body. Get ('id ')
'1'
You can also
>>> Attrs = body. attrib
>>> Attrs. Get ('id ')
'1'
4. Access Element Content
>>> Body. Text
'Abc'
>>> Body. Tail
Text only ends from the current node to the first byte point; tail ends from the last byte to the unknown node.
Access all text information of the current node
>>> Body. XPath ('text ()')
['Abc', 'def ', 'ghi']
Access all text information of the current node and subnode
>>> Body. XPath ('// text ()')
['Abc', '000000', 'def', '000000', 'ghi']
It seems that all text information in this document is returned.
Body. text_content () returns all text information of the current node.
5. Support for xpath
All DIV elements
>>> For ele in Dom. XPath ('// Div '):
... Print ELE. Tag
...
Div
Div
Id = "1" element
>>> Dom. XPath ('// * [@ ID = "1"]') [0]. Tag
'Body'
1st divs under the body
>>> Dom. XPath ('body/Div [1] ') [0]. Tag
'Div'
Refer:
Lxml official documentation: http://codespeak.net/lxml/
Performance of htmlparser: http://blog.ianbicking.org/2008/03/30/python-html-parser-performance/