Introduction and use of the python HTML Parser library lxml (Quick Start)

Source: Internet
Author: User
Tags xpath

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.