Python's lxml module

Source: Internet
Author: User

Environment: python2.7

Installing the lxml module

Pip Install lxml


from lxml import etreetext =  ' <div>    <ul>          <li class= "item-0" ><a href= "link1.html" > First item</a></li>         <li class= " Item-1 "><a href=" link2.html ">second item</a></li>          <li class= "item-inactive" ><a href= "link3.html" >third item </a></li>         <li class= "Item-1" ><a  href= "Link4.html" >fourth item</a></li>          <li class= "item-0" ><a href= "link5.html" >fifth item</a>      </ul> </div> ' Html = etree. HTML (text)    #这是一个地址result  = etree.tOstring (HTML)   #读出来源码, and complete, such as the output of the "body" tag print (result) 


#读取文件里的内容from lxml Import etreehtml = Etree.parse (' hello.html ') result = etree.tostring (HTML, pretty_print=true) print ( Result

Get what's in the Li tag.

. parse ' hello.html ' )

print type ( html Span class= "Crayon-sy" style= "Font-family:inherit;height:inherit;color:rgb (51,51,51);" >)

. xpath '//li ' Span class= "Crayon-sy" style= "Font-family:inherit;height:inherit;color:rgb (51,51,51);" >)

Print Result

Print len(result)

print type ( result Span class= "Crayon-sy" style= "Font-family:inherit;height:inherit;color:rgb (51,51,51);" >)

Print type(result[0])

Reference article:

Note: This blog is just for their own learning lxml module, so did not write well, the following is my QR code

650) this.width=650; "src=" "title=" QRCode _for_gh_0cd223682950_344.jpg "alt=" 2852890398f48fee0c11bb77eaaf87da.jpg "/>

This article from "Tiandaochouqin" blog, declined reprint!

Python's lxml module

Related Article

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: 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.