Python-- lxml用法

目錄

lxml庫(lxml安裝可查看上一篇文章)

Element類

1、節點操作

2、屬性操作

3、文本操作

4、文件解析與輸出

5、ElementPath

6、案例(尤其最後的一篇代碼) 


 

lxml庫(lxml安裝可查看上一篇文章)

  • python的HTML/XML的解析器
  • 官方文檔:   http://lxml.de/index.html
  • 功能:
    • 解析HTML
    • 文件讀取
    • etree和XPath的配合使用

圍繞三個問題:

問題1:有一個XML文件,如何解析
問題2:解析後,如果查找、定位某個標籤
問題3:定位後如何操作標籤,比如訪問屬性、文本內容等

導入模塊,該庫常用的XML處理功能都在lxml.etree中

from lxml import etree  

Element類

Element是XML處理的核心類,Element對象可以直觀的理解爲XML的節點,大部分XML節點的處理都是圍繞該類進行的。這部分包括三個內容:節點的操作、節點屬性的操作、節點內文本的操作。

1、節點操作

1、創建Element對象
使用Element方法,參數即節點名稱。

>>> root = etree.Element('root')
>>> print(root)
<Element root at 0x2da0708>

2、獲取節點名稱
使用tag屬性,獲取節點的名稱。

>>> print(root.tag)
root

3、輸出XML內容
使用tostring方法輸出XML內容,參數爲Element對象。

>>> print(etree.tostring(root))
b'<root><child1/><child2/><child3/></root>'

4、添加子節點
使用SubElement方法創建子節點,第一個參數爲父節點(Element對象),第二個參數爲子節點名稱。

>>> child1 = etree.SubElement(root, 'child1')
>>> child2 = etree.SubElement(root, 'child2')
>>> child3 = etree.SubElement(root, 'child3')

5、刪除子節點
使用remove方法刪除指定節點,參數爲Element對象。clear方法清空所有節點。

>>> root.remove(child1)  # 刪除指定子節點
>>> print(etree.tostring(root))
b'<root><child2/><child3/></root>'
>>> root.clear()  # 清除所有子節點
>>> print(etree.tostring(root))
b'<root/>'

6、以列表的方式操作子節點
可以將Element對象的子節點視爲列表進行各種操作:

>>> child = root[0]  # 下標訪問
>>> print(child.tag)
child1

>>> print(len(root))  # 子節點數量
3

>>> root.index(child2)  # 獲取索引號
1

>>> for child in root:  # 遍歷
...     print(child.tag)
child1
child2
child3

>>> root.insert(0, etree.Element('child0'))  # 插入
>>> start = root[:1]  # 切片
>>> end = root[-1:]

>>> print(start[0].tag)
child0
>>> print(end[0].tag)
child3

>>> root.append( etree.Element('child4') )  # 尾部添加
>>> print(etree.tostring(root))
b'<root><child0/><child1/><child2/><child3/><child4/></root>'

7、獲取父節點
使用getparent方法可以獲取父節點。

>>> print(child1.getparent().tag)
root

2、屬性操作

屬性是以key-value的方式存儲的,就像字典一樣。

1、創建屬性

可以在創建Element對象時同步創建屬性,第二個參數即爲屬性名和屬性值:

>>> root = etree.Element('root', interesting='totally')
>>> print(etree.tostring(root))
b'<root interesting="totally"/>'
也可以使用set方法給已有的Element對象添加屬性,兩個參數分別爲屬性名和屬性值:

>>> root.set('hello', 'Huhu')
>>> print(etree.tostring(root))
b'<root interesting="totally" hello="Huhu"/>'

2、獲取屬性

屬性是以key-value的方式存儲的,就像字典一樣。直接看例子

# get方法獲得某一個屬性值
>>> print(root.get('interesting'))
totally

# keys方法獲取所有的屬性名
>>> sorted(root.keys())
['hello', 'interesting']

# items方法獲取所有的鍵值對
>>> for name, value in sorted(root.items()):
...     print('%s = %r' % (name, value))
hello = 'Huhu'
interesting = 'totally'

也可以用attrib屬性一次拿到所有的屬性及屬性值存於字典中:

>>> attributes = root.attrib
>>> print(attributes)
{'interesting': 'totally', 'hello': 'Huhu'}

>>> attributes['good'] = 'Bye'  # 字典的修改影響節點
>>> print(root.get('good'))
Bye

3、文本操作

標籤及標籤的屬性操作介紹完了,最後就剩下標籤內的文本了。可以使用text和tail屬性、或XPath的方式來訪問文本內容。 

1、text和tail屬性

一般情況,可以用Element的text屬性訪問標籤的文本。

>>> root = etree.Element('root')
>>> root.text = 'Hello, World!'
>>> print(root.text)
Hello, World!
>>> print(etree.tostring(root))
b'<root>Hello, World!</root>'```

XML的標籤一般是成對出現的,有開有關,但像HTML則可能出現單一的標籤,如下面這段代碼中的`<br/>`

`<html><body>Text<br/>Tail</body></html>`  

Element類提供了tail屬性支持單一標籤的文本獲取。
```python
>>> html = etree.Element('html')
>>> body = etree.SubElement(html, 'body')
>>> body.text = 'Text'
>>> print(etree.tostring(html))
b'<html><body>Text</body></html>'

>>> br = etree.SubElement(body, 'br')
>>> print(etree.tostring(html))
b'<html><body>Text<br/></body></html>'

# tail僅在該標籤後面追加文本
>>> br.tail = 'Tail'
>>> print(etree.tostring(br))
b'<br/>Tail'

>>> print(etree.tostring(html))
b'<html><body>Text<br/>Tail</body></html>'

# tostring方法增加method參數,過濾單一標籤,輸出全部文本
>>> print(etree.tostring(html, method='text'))
b'TextTail'

2、XPath方式

# 方式一:過濾單一標籤,返回文本
>>> print(html.xpath('string()'))
TextTail

# 方式二:返回列表,以單一標籤爲分隔
>>> print(html.xpath('//text()'))
['Text', 'Tail']

方法二獲得的列表,每個元素都會帶上它所屬節點及文本類型信息,如下:

>>> texts = html.xpath('//text()'))

>>> print(texts[0])
Text
# 所屬節點
>>> parent = texts[0].getparent()  
>>> print(parent.tag)
body

>>> print(texts[1], texts[1].getparent().tag)
Tail br

# 文本類型:是普通文本還是tail文本
>>> print(texts[0].is_text)
True
>>> print(texts[1].is_text)
False
>>> print(texts[1].is_tail)
True

4、文件解析與輸出

這部分講述如何將XML文件解析爲Element對象,以及如何將Element對象輸出爲XML文件。

1. 文件解析

文件解析常用的有fromstring、XML和HTML三個方法。接受的參數都是字符串。

>>> xml_data = '<root>data</root>'

# fromstring方法
>>> root1 = etree.fromstring(xml_data)
>>> print(root1.tag)
root
>>> print(etree.tostring(root1))
b'<root>data</root>'

# XML方法,與fromstring方法基本一樣
>>> root2 = etree.XML(xml_data)
>>> print(root2.tag)
root
>>> print(etree.tostring(root2))
b'<root>data</root>'

# HTML方法,如果沒有<html>和<body>標籤,會自動補上
>>> root3 = etree.HTML(xml_data)
>>> print(root3.tag)
html
>>> print(etree.tostring(root3))
b'<html><body><root>data</root></body></html>'

2. 輸出

輸出其實就是前面一直在用的tostring方法了,這裏補充xml_declaration和encoding兩個參數,前者是XML聲明,後者是指定編碼。

>>> root = etree.XML('<root><a><b/></a></root>')

>>> print(etree.tostring(root))
b'<root><a><b/></a></root>'

# XML聲明
>>> print(etree.tostring(root, xml_declaration=True))
b"<?xml version='1.0' encoding='ASCII'?>\n<root><a><b/></a></root>"

# 指定編碼
>>> print(etree.tostring(root, encoding='iso-8859-1'))
b"<?xml version='1.0' encoding='iso-8859-1'?>\n<root><a><b/></a></root>"

5、ElementPath

      講ElementPath前,需要引入ElementTree類,一個ElementTree對象可理解爲一個完整的XML樹,每個節點都是一個Element對象。而ElementPath則相當於XML中的XPath。用於搜索和定位Element元素。

        這裏介紹兩個常用方法,可以滿足大部分搜索、查詢需求,它們的參數都是XPath語句(關於XPath的學習可以查看我之前的一片文章):
findall():返回所有匹配的元素,返回列表
find():返回匹配到的第一個元素

>>> root = etree.XML("<root><a x='123'>aText<b/><c/><b/></a></root>")

# 查找第一個b標籤
>>> print(root.find('b'))
None
>>> print(root.find('a').tag)
a

# 查找所有b標籤,返回Element對象組成的列表
>>> [ b.tag for b in root.findall('.//b') ]
['b', 'b']

# 根據屬性查詢
>>> print(root.findall('.//a[@x]')[0].tag)
a
>>> print(root.findall('.//a[@y]'))
[]

以上內容大多來自此原文:Python lxml教程-SKYue

6、案例(尤其最後的一篇代碼) 

  • 解析HTML,案例1.py
  • 文件讀取,案例2.html, 案例2.py
  • etree和XPath的配合使用, 案例3.py

案例1.py

'''
安裝lxml
'''
from lxml import etree

'''
用lxml來解析HTML代碼
'''

text = '''
<div>
    <ul>
        <li class="item-0"> <a href="0.html"> first item </a></li>
        <li class="item-1"> <a href="1.html"> first item </a></li>
        <li class="item-2"> <a href="2.html"> first item </a></li>
        <li class="item-3"> <a href="3.html"> first item </a></li>
        <li class="item-4"> <a href="4.html"> first item </a></li>
        <li class="item-5"> <a href="5.html"> first item </a>
    </ul>
</div>
'''

# 利用etree.HTML把字符串解析成HTML文檔
html = etree.HTML(text)
s = etree.tostring(html)
print(s)

案例2.html

<?xml version="1.0" encoding="utf-8"?>

<bookstore>
    <book category="cooking">
        <title lang="en">Everyday Italian</title>
        <author>Gidada De</author>
        <year>2018</year>
        <price>23</price>
    </book>

    <book category="education">
        <title lang="en">Python is Python</title>
        <author>Food War</author>
        <year>2008</year>
        <price>83</price>
    </book>

    <book category="sport">
        <title lang="en">Running</title>
        <author>Klaus Kuka</author>
        <year>2010</year>
        <price>43</price>
    </book>

</bookstore>

案例2.py 

from lxml import etree

# 只能讀取xml格式內容,html報錯
html = etree.parse("./v30.html")

rst = etree.tostring(html, pretty_print=True)
print(rst)

案例3.py 

from lxml import etree

# 只能讀取xml格式內容,html報錯
html = etree.parse("./v30.html")
print(type(html))

rst = html.xpath('//book')
print(type(rst))
print(rst)

# xpath的意識是,查找帶有category屬性值爲sport的book元素
rst = html.xpath('//book[@category="sport"]')
print(type(rst))
print(rst)

# xpath的意識是,查找帶有category屬性值爲sport的book元素下的year元素
rst = html.xpath('//book[@category="sport"]/year')
rst = rst[0]
print(type(rst))
print(rst.tag)
print(rst.text)

 

目前有很多xml,html文檔的parser,如標準庫的xml.etree , beautifulsoup  ,  還有lxml. 都用下來感覺lxml不錯,速度也還行,就他了.

圍繞三個問題:

  • 問題1:有一個XML文件,如何解析
  • 問題2:解析後,如果查找、定位某個標籤
  • 問題3:定位後如何操作標籤,比如訪問屬性、文本內容等

這些操作應該算是比較基礎的,還可以自己在網上查找相關教程,官網更詳細一點,進階xpath語法,要在以後操作xml文件和html文件用上.

#!/usr/bin/python
# coding=utf-8
# __author__='dahu'
#
'''
Element是XML處理的核心類,
Element對象可以直觀的理解爲XML的節點,大部分XML節點的處理都是圍繞該類進行的。
這部分包括三個內容:節點的操作、節點屬性的操作、節點內文本的操作。
'''
from lxml import etree

# 1.創建element
root = etree.Element('root')
print root, root.tag

# 2.添加子節點
child1 = etree.SubElement(root, 'child1')
child2 = etree.SubElement(root, 'child2')

# 3.刪除子節點
# root.remove(child2)

# 4.刪除所有子節點
# root.clear()

# 5.以列表的方式操作子節點
print(len(root))
print root.index(child1)  # 索引號
root.insert(0, etree.Element('child3'))  # 按位置插入
root.append(etree.Element('child4'))  # 尾部添加

# 6.獲取父節點
print(child1.getparent().tag)
# print root[0].getparent().tag   #用列表獲取子節點,再獲取父節點
'''以上都是節點操作'''

# 7.創建屬性
# root.set('hello', 'dahu')   #set(屬性名,屬性值)
# root.set('hi', 'qing')

# 8.獲取屬性
# print(root.get('hello'))    #get方法
# print root.keys(),root.values(),root.items()    #參考字典的操作
# print root.attrib           #直接拿到屬性存放的字典,節點的attrib,就是該節點的屬性
'''以上是屬性的操作'''

# 9.text和tail屬性
# root.text = 'Hello, World!'
# print root.text

# 10.test,tail和text的結合
html = etree.Element('html')
html.text = 'html.text'
body = etree.SubElement(html, 'body')
body.text = 'wo ai ni'
child = etree.SubElement(body, 'child')
child.text='child.text' #一般情況下,如果一個節點的text沒有內容,就只有</>符號,如果有內容,纔會<>,</>都有
child.tail = 'tails'  # tail是在標籤後面追加文本
print(etree.tostring(html))
# print(etree.tostring(html, method='text'))  # 只輸出text和tail這種文本文檔,輸出的內容連在一起,不實用

#11.Xpath方式
# print(html.xpath('string()'))   #這個和上面的方法一樣,只返回文本的text和tail
print(html.xpath('//text()'))   #這個比較好,按各個文本值存放在列表裏面
tt=html.xpath('//text()')
print tt[0].getparent().tag     #這個可以,首先我可以找到存放每個節點的text的列表,然後我再根據text找相應的節點
# for i in tt:
#     print i,i.getparent().tag,'\t',

#12.判斷文本類型
print tt[0].is_text,tt[-1].is_tail  #判斷是普通text文本,還是tail文本
'''以上都是文本的操作'''

#13.字符串解析,fromstring方式
xml_data = '<html>html.text<body>wo ai ni<child>child.text</child>tails</body></html>'
root1=etree.fromstring(xml_data)    #fromstring,字面意思,直接來源字符串
# print root1.tag
# print etree.tostring(root1)

#14.xml方式
root2 = etree.XML(xml_data)     #和fromstring基本一樣,
print etree.tostring(root2)

#15.文件類型解析
tree =etree.parse('text')   #文件解析成元素樹
root3 = tree.getroot()      #獲取元素樹的根節點
print etree.tostring(root3,pretty_print=True)

parser= etree.XMLParser(remove_blank_text=True) #去除xml文件裏的空行
root = etree.XML("<root>  <a/>   <b>  </b>     </root>",parser)
print etree.tostring(root)

#16.html方式
xml_data1='<root>data</root>'
root4 = etree.HTML(xml_data1)
print(etree.tostring(root4))#HTML方法,如果沒有<html>和<body>標籤,會自動補上
#注意,如果是需要補全的html格式:這樣處理哦
with open("quotes-1.html",'r')as f:
    a=H.document_fromstring(f.read().decode("utf-8"))

for i in  a.xpath('//div[@class="quote"]/span[@class="text"]/text()'):
    print i

#17.輸出內容,輸出xml格式
print etree.tostring(root)
print(etree.tostring(root, xml_declaration=True,pretty_print=True,encoding='utf-8'))#指定xml聲明和編碼
'''以上是文件IO操作'''

#18.findall方法
root = etree.XML("<root><a x='123'>aText<b/><c/><b/></a></root>")
print(root.findall('a')[0].text)#findall操作返回列表
print(root.find('.//a').text)   #find操作就相當與找到了這個元素節點,返回匹配到的第一個元素
print(root.find('a').text)
print [ b.text for b in root.findall('.//a') ]    #配合列表解析,相當帥氣!
print(root.findall('.//a[@x]')[0].tag)  #根據屬性查詢
'''以上是搜索和定位操作'''
print(etree.iselement(root))
print root[0] is root[1].getprevious()  #子節點之間的順序
print root[1] is root[0].getnext()
'''其他技能'''
# 遍歷元素數
root = etree.Element("root")
etree.SubElement(root, "child").text = "Child 1"
etree.SubElement(root, "child").text = "Child 2"
etree.SubElement(root, "another").text = "Child 3"
etree.SubElement(root[0], "childson").text = "son 1"
# for i in root.iter():   #深度遍歷
# for i in root.iter('child'):    #只迭代目標值
#     print i.tag,i.text
# print etree.tostring(root,pretty_print=True)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章