建設(shè)部四庫(kù)一平臺(tái)查詢網(wǎng)站電商平臺(tái)有哪些?
第一章XML
一、xml簡(jiǎn)介
1.什么是XML?
1,XML指可擴(kuò)展標(biāo)記語(yǔ)言
2,XML是一種標(biāo)記語(yǔ)言,類(lèi)似于HTML
3,XML的設(shè)計(jì)宗旨是傳輸數(shù)據(jù),而非顯示數(shù)據(jù)
4,XML標(biāo)簽需要我們自己自定義
5,XML被設(shè)計(jì)為具有自我描述性
2.XML和HTML的區(qū)別?
1,XML被設(shè)計(jì)為傳輸和存儲(chǔ)數(shù)據(jù),其焦點(diǎn)是數(shù)據(jù)的內(nèi)容
2,HTML是顯示數(shù)據(jù)以及如何更好的顯示數(shù)據(jù)
3.XML文檔示例
<?xml version="1.0" encoding="utf-8"?><bookstore><book category="cooking"><title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price></book> <book category="children"><title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price></book> <book category="web"><title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price></book><book category="web" cover="paperback"><title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price></book></bookstore>
注意:這上面的標(biāo)簽都是自定義的
二、XML的節(jié)點(diǎn)關(guān)系
1.父(parent)
每個(gè)元素及屬性都有一個(gè)父
下面這個(gè)XML例子中,book元是title,author,year,price元素的父
<?xml version="1.0" encoding="utf-8"?>
<book>
? <title>Harry Potter</title>
? <author>J K. Rowling</author>
? <year>2005</year>
? <price>29.99</price>
</book>
2.子(children)
元素節(jié)點(diǎn)可能有零個(gè),一個(gè)或者多個(gè)子
在下面的例子中 title,author,year,price都是book元素的子
<?xml version="1.0" encoding="utf-8"?>
<book>
? <title>Harry Potter</title>
? <author>J K. Rowling</author>
? <year>2005</year>
? <price>29.99</price>
</book>
3.同胞(sibling)
擁有相同的父的節(jié)點(diǎn)
在下面例子中 title,author,year,price元素都是同胞
<?xml version="1.0" encoding="utf-8"?>
<book>
? <title>Harry Potter</title>
? <author>J K. Rowling</author>
? <year>2005</year>
? <price>29.99</price>
</book>
4.先輩(ancestor)
某節(jié)點(diǎn)的父,父的父,等等
下面例子中,title元素的先輩是book和bookstore
<?xml version="1.0" encoding="utf-8"?>
<bookstore>
? ? <book>
? ? ? <title>Harry Potter</title>
? ? ? <author>J K. Rowling</author>
? ? ? <year>2005</year>
? ? ? <price>29.99</price>
? ? </book></bookstore>
5.后代
某節(jié)點(diǎn)的子,子的子,等等
下面例子中,bookstore后代是book,title,author,year,price元素
<?xml version="1.0" encoding="utf-8"?>
<bookstore>
<book>
? <title>Harry Potter</title>
? <author>J K. Rowling</author>
? <year>2005</year>
? <price>29.99</price>
</book>
</bookstore>
第二章 xpath
XPath原理:先將HTML文檔轉(zhuǎn)為XML文檔,再用XPath查找HTML節(jié)點(diǎn)或元素
一、xpath簡(jiǎn)介
xpth解析
(1)本地文件 ???????????????????????????????????????? ???????????????????????????????????????????????? etree.parse
(2)服務(wù)器響應(yīng)的數(shù)據(jù) ???????????????? response.read().decode('utf-8') ???????? etree.HTML()
示例1:
1.本地文件解析
解析_xpath的基本使用.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"/><title>Title</title>
</head>
<body><ul><li id="l1" class="c1">北京</li><li id="l2">上海</li><li class="c3">深圳</li><li id="c3">武漢</li><li id="c4">廣州</li></ul><ul><li>大連</li><li>錦州</li><li>沈陽(yáng)</li></ul>
</body>
</html>
#解析'解析——xpath的基本使用.html‘本地文件 tree=etree.parse('解析_xpath的基本使用.html') print(tree)#<lxml.etree._ElementTree object at 0x000001FC65813F88>
二、節(jié)點(diǎn)選取
1.選取節(jié)點(diǎn)
XPath使用路徑表達(dá)式來(lái)選取XML文檔中的節(jié)點(diǎn)或者節(jié)點(diǎn)集,這些路徑表達(dá)式和我們?cè)诔R?guī)的電腦文件系統(tǒng)里看到的表達(dá)式非常相似。
最常用的路徑表達(dá)式:
表達(dá)式 | 描述 |
---|---|
nodename | 選取此節(jié)點(diǎn)的所有子節(jié)點(diǎn)。 |
/ | 從根節(jié)點(diǎn)選取。 |
// | 從匹配選擇的當(dāng)前節(jié)點(diǎn)選擇文檔中的節(jié)點(diǎn),而不考慮它們的位置。 |
. | 選取當(dāng)前節(jié)點(diǎn)。 |
.. | 選取當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)。 |
@ | 選取屬性。 |
例如:
bookstore | 選取 bookstore 元素的所有子節(jié)點(diǎn)。 | ? |
bookstore/book | 選取屬于 bookstore 的子元素的所有 book 元素。 | ? |
//book | 選取所有 book 子元素,而不管它們?cè)谖臋n中的位置。 | ? |
bookstore//book | 選擇屬于 bookstore 元素的后代的所有 book 元素,而不管它們位于 bookstore 之下的什么位置。 | ? |
//@lang | 選取名為 lang 的所有屬性。 | ? |
2.選取未知節(jié)點(diǎn)
通配符 | 描述 |
---|---|
* | 匹配任何元素節(jié)點(diǎn)。 |
@* | 匹配任何屬性節(jié)點(diǎn)。 |
node() | 匹配任何類(lèi)型的節(jié)點(diǎn)。 |
比如:
路徑表達(dá)式 | 結(jié)果 |
---|---|
/bookstore/* | 選取 bookstore 元素的所有子元素。 |
//* | 選取文檔中的所有元素。 |
html/node()/meta/@* | 選擇html下面任意節(jié)點(diǎn)下的meta節(jié)點(diǎn)的所有屬性 |
//title[@*] | 選取所有帶有屬性的 title 元素。 |
?3.選取若干路徑
通過(guò)在路徑表達(dá)式中使用“|”運(yùn)算符,您可以選取若干個(gè)路徑。
比如:?
路徑表達(dá)式 | 結(jié)果 |
---|---|
//book/title | //book/price | 選取 book 元素的所有 title 和 price 元素。 |
//title | //price | 選取文檔中的所有 title 和 price 元素。 |
/bookstore/book/title | //price | 選取屬于 bookstore 元素的 book 元素的所有 title 元素,以及文檔中所有的 price 元素。 |
三、路徑查詢
tree.xpath('xpath路徑')
1? ? ? ? ?//:查找所有的子孫節(jié)點(diǎn),不考慮層級(jí)關(guān)系
2? ? ? ? ?/:找直接子節(jié)點(diǎn)
1.查找ul下面的li
li_list=tree.xpath('//body/ul/li') # #判斷列表的長(zhǎng)度 print(li_list) print(len(li_list))
2.謂詞查詢
如:
//div[@id]
//div[@id='屬性值']
#查找所有有id屬性的li標(biāo)簽 #text()獲取標(biāo)簽中的內(nèi)容 li_list=tree.xpath('//ul/li[@id]') li_list1=tree.xpath('//ul/li[@id]/text()')#['北京', '上海'] #查找id=l1的li標(biāo)簽 注意引號(hào)問(wèn)題 li_list2=tree.xpath('//ul/li[@id="l1"]/text()') print(li_list) print(li_list1) print(li_list2)
3.屬性查詢
如://@class
#查找id為l1的li標(biāo)簽的class的屬性值 li_list=tree.xpath('//ul/li[@id="l1"]/@class')#c1 print(li_list)
4.模糊查詢
如:
//div[contains(@id,"he")]
//div[starts-with(@id,"he")]
#查找id中包含l的li標(biāo)簽 li_list=tree.xpath('//ul/li[contains(@id,"l")]/text()')#['北京', '上海'] print(li_list)
5.內(nèi)容查詢
如://div/h1/text()
#查詢id的值以c開(kāi)頭的標(biāo)簽 li_list=tree.xpath('//ul/li[starts-with(@id,"c")]/text()')#['武漢', '廣州'] print(li_list)
6.邏輯運(yùn)算
如:
//div[@id="head" and @class="s_down"]
//title|//price
#查詢id為l1和class為c1的標(biāo)簽 li_list=tree.xpath('//ul/li[@id="l1" and @class="c1"]/text()')#['北京'] li_list1=tree.xpath('//ul/li[@id="l1"]/text() | //ul/li[@id="l2"]/text()')#['北京', '上海'] print(li_list) print(len(li_list)) print(li_list1) print(len(li_list1))
四、代碼示例
html = '''
<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></div>
'''# 1,使用lxml的etree類(lèi)
from lxml import etree#,2,利用etree.HTML()構(gòu)造一個(gè)xpath解析對(duì)象(轉(zhuǎn)為xml文檔)
xml_doc=etree.HTML(html)
print(xml_doc)
print('-----'*10)# etree.tostring()輸出轉(zhuǎn)換后的html代碼,
html_doc = etree.tostring(xml_doc)
print(html_doc) #自動(dòng)補(bǔ)全了body,html標(biāo)簽
print(type(html_doc)) # bytes類(lèi)型
# print('-----'*10)
print(html_doc.decode()) # 利用decode()方法將其轉(zhuǎn)成str類(lèi)型,
print(type(html_doc.decode()))
注意:
1,只要涉及到條件,加 []
2,只要獲取屬性值,加 @
3,通過(guò)text()取內(nèi)容
?