本章使用以下 XML 文檔作為示例。
<?xml version="1.0" encoding="utf8"?><bookstore> <book> <titlelang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <titlelang="eng">Learning XML</title> <price>39.95</price> </book></bookstore>
選取節(jié)點(diǎn)以下為基本路徑的表達(dá)方式,記住 XPath 的路徑表達(dá)式都是基于某個(gè)節(jié)點(diǎn)之上的,例如最初的當(dāng)前節(jié)點(diǎn)一般是根節(jié)點(diǎn),這與 Linux 下路徑切換原理是一樣的。
表達(dá)式 | 描述 |
nodename | 選取已匹配節(jié)點(diǎn)下名為 nodename 的子元素節(jié)點(diǎn)。 |
/ | 如果以 / 開(kāi)頭,表示從根節(jié)點(diǎn)作為選取起點(diǎn)。 |
// | 在已匹配節(jié)點(diǎn)后代中選取節(jié)點(diǎn),不考慮目標(biāo)節(jié)點(diǎn)的位置。 |
. | 選取當(dāng)前節(jié)點(diǎn)。 |
.. | 選取當(dāng)前節(jié)點(diǎn)的父元素節(jié)點(diǎn)。 |
@ | 選取屬性。 |
>>> from lxml import etree
>>> xml = """<?xml version="1.0" encoding="utf8"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>"""# 得到根節(jié)點(diǎn)>>> root = etree.fromstring(xml)
>>> print root
<Element bookstore at 0x2c9cc88>
# 選取所有book子元素>>> root.xpath('book')
[<Element book at 0x2d88878>, <Element book at 0x2d888c8>]
# 選取根節(jié)點(diǎn)bookstore>>> root.xpath('/bookstore')
[<Element bookstore at 0x2c9cc88>]
# 選取所有book子元素的title子元素>>> root.xpath('book/title')
[<Element title at 0x2d88878>, <Element title at 0x2d888c8>]
# 以根節(jié)點(diǎn)為始祖,選取其后代中的title元素>>> root.xpath('//title')
[<Element title at 0x2d88878>, <Element title at 0x2d888c8>]
# 以book子元素為始祖,選取后代中的price元素>>> root.xpath('book//price')
[<Element price at 0x2ca20a8>, <Element price at 0x2d88738>]
# 以根節(jié)點(diǎn)為始祖,選取其后代中的lang屬性值>>> root.xpath('//@lang')
['eng', 'eng']
預(yù)判(Predicates)預(yù)判是用來(lái)查找某個(gè)特定的節(jié)點(diǎn)或者符合某種條件的節(jié)點(diǎn),預(yù)判表達(dá)式位于方括號(hào)中。
# 選取bookstore的第一個(gè)book子元素>>> root.xpath('/bookstore/book[1]')
[<Element book at 0x2ca20a8>]
# 選取bookstore的最后一個(gè)book子元素>>> root.xpath('/bookstore/book[last()]')
[<Element book at 0x2d88878>]
# 選取bookstore的倒數(shù)第二個(gè)book子元素>>> root.xpath('/bookstore/book[last()-1]')
[<Element book at 0x2ca20a8>]
# 選取bookstore的前兩個(gè)book子元素>>> root.xpath('/bookstore/book[position()<3]')
[<Element book at 0x2ca20a8>, <Element book at 0x2d88878>]
# 以根節(jié)點(diǎn)為始祖,選取其后代中含有l(wèi)ang屬性的title元素>>> root.xpath('//title[@lang]')
[<Element title at 0x2d888c8>, <Element title at 0x2d88738>]
# 以根節(jié)點(diǎn)為始祖,選取其后代中含有l(wèi)ang屬性并且值為eng的title元素>>> root.xpath("http://title[@lang='eng']")
[<Element title at 0x2d888c8>, <Element title at 0x2d88738>]
# 選取bookstore子元素book,條件是book的price子元素要大于35>>> root.xpath("/bookstore/book[price>35.00]")
[<Element book at 0x2ca20a8>]
# 選取bookstore子元素book的子元素title,條件是book的price子元素要大于35>>> root.xpath("/bookstore/book[price>35.00]/title")
[<Element title at 0x2d888c8>]
通配符
通配符 | 描述 |
* | 匹配任何元素。 |
@* | 匹配任何屬性。 |
node() | 匹配任何類(lèi)型的節(jié)點(diǎn)。 |
# 選取 bookstore 所有子元素>>> root.xpath('/bookstore/*')
[<Element book at 0x2d888c8>, <Element book at 0x2ca20a8>]
# 選取根節(jié)點(diǎn)的所有后代元素>>> root.xpath('//*')
[<Element bookstore at 0x2c9cc88>, <Element book at 0x2d888c8>, <Element title at 0x2d88738>, <Element price at 0x2d88878>, <Element book at 0x2ca20a8>, <Element title at 0x2d88940>, <Element price at 0x2d88a08>]
# 選取根節(jié)點(diǎn)的所有具有屬性節(jié)點(diǎn)的title元素>>> root.xpath('//title[@*]')
[<Element title at 0x2d88738>, <Element title at 0x2d88940>]
# 選取當(dāng)前節(jié)點(diǎn)下所有節(jié)點(diǎn)。'
' 是文本節(jié)點(diǎn)。>>> root.xpath('node()')
['
', <Element book at 0x2d888c8>, '
', <Element book at 0x2d88878>, '
']
# 選取根節(jié)點(diǎn)所有后代節(jié)點(diǎn),包括元素、屬性、文本。>>> root.xpath('//node()')
[<Element bookstore at 0x2c9cc88>, '
', <Element book at 0x2d888c8>, '
', <Element title at 0x2d88738>, 'Harry Potter', '
', <Element price at 0x2d88940>, '29.99', '
', '
', <Element book at 0x2d88878>, '
', <Element title at 0x2ca20a8>, 'Learning XML', '
', <Element price at 0x2d88a08>, '39.95', '
', '
']
或條件選取使用 "|" 運(yùn)算符,你可以選取符合“或”條件的若干路徑。
# 選取所有book的title元素或者price元素>>> root.xpath('//book/title|//book/price')
[<Element title at 0x2d88738>, <Element price at 0x2d88940>, <Element title at 0x2ca20a8>, <Element price at 0x2d88a08>]
# 選擇所有title或者price元素>>> root.xpath('//title|//price')
[<Element title at 0x2d88738>, <Element price at 0x2d88940>, <Element title at 0x2ca20a8>, <Element price at 0x2d88a08>]
# 選擇book子元素title或者全部的price元素>>> root.xpath('/bookstore/book/title|//price')
[<Element title at 0x2d88738>, <Element price at 0x2d88940>, <Element title at 0x2ca20a8>, <Element price at 0x2d88a08>]
分享文章:[XPath]XPath與lxml(二)XPath語(yǔ)法-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://sd-ha.com/article2/ccodic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站導(dǎo)航、網(wǎng)站收錄、網(wǎng)站排名、建站公司、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容