Python 之lxml解析模塊


lxml 是 一個HTML/XML的解析器,主要的功能是如何解析和提取 HTML/XML 數據。

一、lxml示例

1、初步

# 使用 lxml 的 etree 庫
from lxml import etree 

text = '''
<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> # 注意,此處缺少一個 </li> 閉合標簽
     </ul>
 </div>
'''

#利用etree.HTML,將字符串解析為HTML文檔
html = etree.HTML(text) 

# 按字符串序列化HTML文檔
result = etree.tostring(html) 

print(result)

結果

<html><body>
<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></li>
</ul>
 </div>
</body></html>

2、從文件里讀取內容

from lxml import etree

# 讀取外部文件 hello.html
html = etree.parse('./hello.html')
result = etree.tostring(html, pretty_print=True)

print(result)

3、html內容

<html><body>
<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></li>
</ul>
 </div>
</body></html>

@1、獲取所有的 <li> 標簽

from lxml import etree

html = etree.parse('hello.html')
print type(html)  # 顯示etree.parse() 返回類型

result = html.xpath('//li')

print result  # 打印<li>標簽的元素集合
print len(result)
print type(result)
print type(result[0])


結果是
<type 'lxml.etree._ElementTree'>
[<Element li at 0x1014e0e18>, <Element li at 0x1014e0ef0>, <Element li at 0x1014e0f38>, <Element li at 0x1014e0f80>, <Element li at 0x1014e0fc8>]
5
<type 'list'>
<type 'lxml.etree._Element'>

@2、繼續獲取<li> 標簽的所有 class屬性

from lxml import etree

html = etree.parse('hello.html')
result = html.xpath('//li/@class')

print result

結果是
['item-0', 'item-1', 'item-inactive', 'item-1', 'item-0']

@3、繼續獲取<li>標簽下href為 link1.html 的 <a> 標簽

from lxml import etree

html = etree.parse('hello.html')
result = html.xpath('//li/a[@href="link1.html"]')

print result

運行結果

[<Element a at 0x10ffaae18>]

 @4、獲取<li> 標簽下的所有 <span> 標簽

from lxml import etree

html = etree.parse('hello.html')

#result = html.xpath('//li/span')
#注意這么寫是不對的:
#因為 / 是用來獲取子元素的,而 <span> 並不是 <li> 的子元素,所以,要用雙斜杠

result = html.xpath('//li//span')

print result

運行結果

[<Element span at 0x10d698e18>]

@5、獲取 <li> 標簽下的<a>標簽里的所有 class

from lxml import etree

html = etree.parse('hello.html')
result = html.xpath('//li/a//@class')

print result

運行結果

['blod']

@6、獲取最后一個 <li> 的 <a> 的 href

from lxml import etree

html = etree.parse('hello.html')

result = html.xpath('//li[last()]/a/@href')
# 謂語 [last()] 可以找到最后一個元素

print result

運行結果

['link5.html']

@7、獲取倒數第二個元素的內容

from lxml import etree

html = etree.parse('hello.html')
result = html.xpath('//li[last()-1]/a')

# text 方法可以獲取元素內容
print result[0].text

運行結果

fourth item

@8、獲取 class 值為 bold 的標簽名

from lxml import etree

html = etree.parse('hello.html')

result = html.xpath('//*[@class="bold"]')

# tag方法可以獲取標簽名
print result[0].tag

運行結果

span

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM