BS4&xpath的使用
0|1一 簡介
簡單來說,Beautiful Soup是python的一個(gè)庫,最主要的功能是從網(wǎng)頁抓取數(shù)據(jù)。官方解釋如下:
''' Beautiful Soup提供一些簡單的、python式的函數(shù)用來處理導(dǎo)航、搜索、修改分析樹等功能。 它是一個(gè)工具箱,通過解析文檔為用戶提供需要抓取的數(shù)據(jù),因?yàn)楹唵危圆恍枰嗌俅a就可以寫出一個(gè)完整的應(yīng)用程序。 '''
Beautiful Soup 是一個(gè)可以從HTML或XML文件中提取數(shù)據(jù)的Python庫.它能夠通過你喜歡的轉(zhuǎn)換器實(shí)現(xiàn)慣用的文檔導(dǎo)航,查找,修改文檔的方式.Beautiful Soup會(huì)幫你節(jié)省數(shù)小時(shí)甚至數(shù)天的工作時(shí)間.你可能在尋找 Beautiful Soup3 的文檔,Beautiful Soup 3 目前已經(jīng)停止開發(fā),官網(wǎng)推薦在現(xiàn)在的項(xiàng)目中使用Beautiful Soup 4。
安裝
|
1
|
pip3 install beautifulsoup4 |
解析器
Beautiful Soup支持Python標(biāo)準(zhǔn)庫中的HTML解析器,還支持一些第三方的解析器,如果我們不安裝它,則 Python 會(huì)使用 Python默認(rèn)的解析器,lxml 解析器更加強(qiáng)大,速度更快,推薦安裝。
|
1
|
pip3 install lxml |
另一個(gè)可供選擇的解析器是純Python實(shí)現(xiàn)的 html5lib , html5lib的解析方式與瀏覽器相同,可以選擇下列方法來安裝html5lib:
pip install html5lib
解析器對比:
簡單使用
下面的一段HTML代碼將作為例子被多次用到.這是 愛麗絲夢游仙境的 的一段內(nèi)容(以后內(nèi)容中簡稱為 愛麗絲 的文檔):
html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a class="sister" id="link1">Elsie</a>, <a class="sister" id="link2">Lacie</a> and <a class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """
使用BeautifulSoup解析這段代碼,能夠得到一個(gè) BeautifulSoup 的對象
from bs4 import BeautifulSoup soup = BeautifulSoup(html_doc, 'html.parser')
從文檔中找到所有<a>標(biāo)簽的鏈接:
|
1
2
|
for link in soup.find_all('a'): print(link.get('href')) |
從文檔中獲取所有文字內(nèi)容:
print(soup.get_text())
0|1二 標(biāo)簽對象
通俗點(diǎn)講就是 HTML 中的一個(gè)個(gè)標(biāo)簽,Tag 對象與XML或HTML原生文檔中的tag相同:
soup = BeautifulSoup('<b class="boldest">Extremely bold</b>')
tag = soup.b
type(tag)
# <class 'bs4.element.Tag'>
Tag的名字
soup對象再以愛麗絲夢游仙境的html_doc為例,操作文檔樹最簡單的方法就是告訴它你想獲取的tag的name.如果想獲取 <head> 標(biāo)簽,只要用 soup.head :
soup.head # <head><title>The Dormouse's story</title></head> soup.title # <title>The Dormouse's story</title>
這是個(gè)獲取tag的小竅門,可以在文檔樹的tag中多次調(diào)用這個(gè)方法.下面的代碼可以獲取<body>標(biāo)簽中的第一個(gè)<b>標(biāo)簽:
soup.body.b # <b>The Dormouse's story</b>
通過點(diǎn)取屬性的方式只能獲得當(dāng)前名字的第一個(gè)tag:
soup.a # <a class="sister" id="link1">Elsie</a>
如果想要得到所有的<a>標(biāo)簽,或是通過名字得到比一個(gè)tag更多的內(nèi)容的時(shí)候,就需要用到 Searching the tree 中描述的方法,比如: find_all()
soup.find_all('a')
# [<a class="sister" id="link1">Elsie</a>,
# <a class="sister" id="link2">Lacie</a>,
# <a class="sister" id="link3">Tillie</a>]
我們可以利用 soup加標(biāo)簽名輕松地獲取這些標(biāo)簽的內(nèi)容,注意,它查找的是在所有內(nèi)容中的第一個(gè)符合要求的標(biāo)簽。
Tag的name和attributes屬性
Tag有很多方法和屬性,現(xiàn)在介紹一下tag中最重要的屬性: name和attributes
每個(gè)tag都有自己的名字,通過 .name 來獲取:
tag.name
# u'b'
tag['class']
# u'boldest'
tag.attrs
# {u'class': u'boldest'}
tag的屬性可以被添加,刪除或修改. 再說一次, tag的屬性操作方法與字典一樣
tag['class'] = 'verybold'
tag['id'] = 1
tag
# <blockquote class="verybold" id="1">Extremely bold</blockquote>
del tag['class']
del tag['id']
tag
# <blockquote>Extremely bold</blockquote>
tag['class']
# KeyError: 'class'
print(tag.get('class'))
# None
0|1三 遍歷文檔樹
''' 1、用法 2、獲取標(biāo)簽的名稱 3、獲取標(biāo)簽的屬性 4、獲取標(biāo)簽的內(nèi)容 5、嵌套選擇 6、子節(jié)點(diǎn)、子孫節(jié)點(diǎn) 7、父節(jié)點(diǎn)、祖先節(jié)點(diǎn) 8、兄弟節(jié)點(diǎn) '''
#遍歷文檔樹:即直接通過標(biāo)簽名字選擇,特點(diǎn)是選擇速度快,但如果存在多個(gè)相同的標(biāo)簽則只返回第一個(gè) html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p id="my p" class="title"><b id="bbb" class="boldest">The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a class="sister" id="link1">Elsie</a>, <a class="sister" id="link2">Lacie</a> and <a class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ #1、用法 from bs4 import BeautifulSoup soup=BeautifulSoup(html_doc,'lxml') # soup=BeautifulSoup(open('a.html'),'lxml') print(soup.p) #存在多個(gè)相同的標(biāo)簽則只返回第一個(gè) print(soup.a) #存在多個(gè)相同的標(biāo)簽則只返回第一個(gè) #2、獲取標(biāo)簽的名稱 print(soup.p.name) #3、獲取標(biāo)簽的屬性 print(soup.p.attrs) #4、獲取標(biāo)簽的內(nèi)容 print(soup.p.string) # p下的文本只有一個(gè)時(shí),取到,否則為None print(soup.p.strings) #拿到一個(gè)生成器對象, 取到p下所有的文本內(nèi)容 print(soup.p.text) #取到p下所有的文本內(nèi)容 for line in soup.stripped_strings: #去掉空白 print(line) ''' 如果tag包含了多個(gè)子節(jié)點(diǎn),tag就無法確定 .string 方法應(yīng)該調(diào)用哪個(gè)子節(jié)點(diǎn)的內(nèi)容, .string 的輸出結(jié)果是 None,如果只有一個(gè)子節(jié)點(diǎn)那么就輸出該子節(jié)點(diǎn)的文本,比如下面的這種結(jié)構(gòu),soup.p.string 返回為None,但soup.p.strings就可以找到所有文本 <p id='list-1'> 哈哈哈哈 <a class='sss'> <span> <h1>aaaa</h1> </span> </a> <b>bbbbb</b> </p> ''' #5、嵌套選擇 print(soup.head.title.string) print(soup.body.a.string) #6、子節(jié)點(diǎn)、子孫節(jié)點(diǎn) print(soup.p.contents) #p下所有子節(jié)點(diǎn) print(soup.p.children) #得到一個(gè)迭代器,包含p下所有子節(jié)點(diǎn) for i,child in enumerate(soup.p.children): print(i,child) print(soup.p.descendants) #獲取子孫節(jié)點(diǎn),p下所有的標(biāo)簽都會(huì)選擇出來 for i,child in enumerate(soup.p.descendants): print(i,child) #7、父節(jié)點(diǎn)、祖先節(jié)點(diǎn) print(soup.a.parent) #獲取a標(biāo)簽的父節(jié)點(diǎn) print(soup.a.parents) #找到a標(biāo)簽所有的祖先節(jié)點(diǎn),父親的父親,父親的父親的父親... #8、兄弟節(jié)點(diǎn) print('=====>') print(soup.a.next_sibling) #下一個(gè)兄弟 print(soup.a.previous_sibling) #上一個(gè)兄弟 print(list(soup.a.next_siblings)) #下面的兄弟們=>生成器對象 print(soup.a.previous_siblings) #上面的兄弟們=>生成器對象
0|1四 搜索文檔樹
BeautifulSoup定義了很多搜索方法,這里著重介紹2個(gè): find() 和 find_all() .其它方法的參數(shù)和用法類似
1、五種過濾器
#搜索文檔樹:BeautifulSoup定義了很多搜索方法,這里著重介紹2個(gè): find() 和 find_all() .其它方法的參數(shù)和用法類似 html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p id="my p" class="title"><b id="bbb" class="boldest">The Dormouse's story</b> </p> <p class="story">Once upon a time there were three little sisters; and their names were <a class="sister" id="link1">Elsie</a>, <a class="sister" id="link2">Lacie</a> and <a class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ from bs4 import BeautifulSoup soup=BeautifulSoup(html_doc,'lxml') #1、五種過濾器: 字符串、正則表達(dá)式、列表、True、方法 #1.1、字符串:即標(biāo)簽名 print(soup.find_all('b')) #1.2、正則表達(dá)式 import re print(soup.find_all(re.compile('^b'))) #找出b開頭的標(biāo)簽,結(jié)果有body和b標(biāo)簽 #1.3、列表:如果傳入列表參數(shù),Beautiful Soup會(huì)將與列表中任一元素匹配的內(nèi)容返回.下面代碼找到文檔中所有<a>標(biāo)簽和<b>標(biāo)簽: print(soup.find_all(['a','b'])) #1.4、True:可以匹配任何值,下面代碼查找到所有的tag,但是不會(huì)返回字符串節(jié)點(diǎn) print(soup.find_all(True)) for tag in soup.find_all(True): print(tag.name) #1.5、方法:如果沒有合適過濾器,那么還可以定義一個(gè)方法,方法只接受一個(gè)元素參數(shù) ,如果這個(gè)方法返回 True 表示當(dāng)前元素匹配并且被找到,如果不是則反回 False def has_class_but_no_id(tag): return tag.has_attr('class') and not tag.has_attr('id') print(soup.find_all(has_class_but_no_id))
2、find_all()
#2、find_all( name , attrs , recursive , text , **kwargs ) #2.1、name: 搜索name參數(shù)的值可以使任一類型的 過濾器 ,字符竄,正則表達(dá)式,列表,方法或是 True . print(soup.find_all(name=re.compile('^t'))) #2.2、keyword: key=value的形式,value可以是過濾器:字符串 , 正則表達(dá)式 , 列表, True . print(soup.find_all(id=re.compile('my'))) print(soup.find_all(href=re.compile('lacie'),id=re.compile('\d'))) #注意類要用class_ print(soup.find_all(id=True)) #查找有id屬性的標(biāo)簽 # 有些tag屬性在搜索不能使用,比如HTML5中的 data-* 屬性: data_soup = BeautifulSoup('<div data-foo="value">foo!</div>','lxml') # data_soup.find_all(data-foo="value") #報(bào)錯(cuò):SyntaxError: keyword can't be an expression # 但是可以通過 find_all() 方法的 attrs 參數(shù)定義一個(gè)字典參數(shù)來搜索包含特殊屬性的tag: print(data_soup.find_all(attrs={"data-foo": "value"})) # [<div data-foo="value">foo!</div>] #2.3、按照類名查找,注意關(guān)鍵字是class_,class_=value,value可以是五種選擇器之一 print(soup.find_all('a',class_='sister')) #查找類為sister的a標(biāo)簽 print(soup.find_all('a',class_='sister ssss')) #查找類為sister和sss的a標(biāo)簽,順序錯(cuò)誤也匹配不成功 print(soup.find_all(class_=re.compile('^sis'))) #查找類為sister的所有標(biāo)簽 #2.4、attrs print(soup.find_all('p',attrs={'class':'story'})) #2.5、text: 值可以是:字符,列表,True,正則 print(soup.find_all(text='Elsie')) print(soup.find_all('a',text='Elsie')) #2.6、limit參數(shù):如果文檔樹很大那么搜索會(huì)很慢.如果我們不需要全部結(jié)果,可以使用 limit 參數(shù)限制返回結(jié)果的數(shù)量.效果與SQL中的limit關(guān)鍵字類似,當(dāng)搜索到的結(jié)果數(shù)量達(dá)到 limit 的限制時(shí),就停止搜索返回結(jié)果 print(soup.find_all('a',limit=2)) #2.7、recursive:調(diào)用tag的 find_all() 方法時(shí),Beautiful Soup會(huì)檢索當(dāng)前tag的所有子孫節(jié)點(diǎn),如果只想搜索tag的直接子節(jié)點(diǎn),可以使用參數(shù) recursive=False . print(soup.html.find_all('a')) print(soup.html.find_all('a',recursive=False)) ''' 像調(diào)用 find_all() 一樣調(diào)用tag find_all() 幾乎是Beautiful Soup中最常用的搜索方法,所以我們定義了它的簡寫方法. BeautifulSoup 對象和 tag 對象可以被當(dāng)作一個(gè)方法來使用,這個(gè)方法的執(zhí)行結(jié)果與調(diào)用這個(gè)對象的 find_all() 方法相同,下面兩行代碼是等價(jià)的: soup.find_all("a") soup("a") 這兩行代碼也是等價(jià)的: soup.title.find_all(text=True) soup.title(text=True) '''
3、find()
#3、find( name , attrs , recursive , text , **kwargs ) find_all() 方法將返回文檔中符合條件的所有tag,盡管有時(shí)候我們只想得到一個(gè)結(jié)果.比如文檔中只有一個(gè)<body>標(biāo)簽,那么使用 find_all() 方法來查找<body>標(biāo)簽就不太合適, 使用 find_all 方法并設(shè)置 limit=1 參數(shù)不如直接使用 find() 方法.下面兩行代碼是等價(jià)的: soup.find_all('title', limit=1) # [<title>The Dormouse's story</title>] soup.find('title') # <title>The Dormouse's story</title> 唯一的區(qū)別是 find_all() 方法的返回結(jié)果是值包含一個(gè)元素的列表,而 find() 方法直接返回結(jié)果. find_all() 方法沒有找到目標(biāo)是返回空列表, find() 方法找不到目標(biāo)時(shí),返回 None . print(soup.find("nosuchtag")) # None soup.head.title 是 tag的名字 方法的簡寫.這個(gè)簡寫的原理就是多次調(diào)用當(dāng)前tag的 find() 方法: soup.head.title # <title>The Dormouse's story</title> soup.find("head").find("title") # <title>The Dormouse's story</title>
4、其他方法
#見官網(wǎng):https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#find-parents-find-parent
5、css選擇器
我們在寫 CSS 時(shí),標(biāo)簽名不加任何修飾,類名前加點(diǎn),id名前加 #,在這里我們也可以利用類似的方法來篩選元素,用到的方法是 soup.select(),返回類型是 list
(1)通過標(biāo)簽名查找
print(soup.select("title")) #[<title>The Dormouse's story</title>]
print(soup.select("b")) #[<b>The Dormouse's story</b>]
(2)通過類名查找
print(soup.select(".sister"))
'''
[<a class="sister" id="link1">Elsie</a>,
<a class="sister" id="link2">Lacie</a>,
<a class="sister" id="link3">Tillie</a>]
'''
(3)通過 id 名查找
print(soup.select("#link1"))
# [<a class="sister" id="link1">Elsie</a>]
(4)組合查找
組合查找即和寫 class 文件時(shí),標(biāo)簽名與類名、id名進(jìn)行的組合原理是一樣的,例如查找 p 標(biāo)簽中,id 等于 link1的內(nèi)容,二者需要用空格分開
print(soup.select("p #link2"))
#[<a class="sister" id="link2">Lacie</a>]
直接子標(biāo)簽查找
print(soup.select("p > #link2"))
# [<a class="sister" id="link2">Lacie</a>]
(5)屬性查找
查找時(shí)還可以加入屬性元素,屬性需要用中括號括起來,注意屬性和標(biāo)簽屬于同一節(jié)點(diǎn),所以中間不能加空格,否則會(huì)無法匹配到。
print(soup.select("a[))
#[<a class="sister" id="link3">Tillie</a>]
select 方法返回的結(jié)果都是列表形式,可以遍歷形式輸出,然后用 get_text() 方法來獲取它的內(nèi)容:
for title in soup.select('a'):
print (title.get_text())
'''
Elsie
Lacie
Tillie
'''
0|1五 修改文檔樹
2|0xpath
2|1xpath簡介
XPath在Python的爬蟲學(xué)習(xí)中,起著舉足輕重的地位,對比正則表達(dá)式 re兩者可以完成同樣的工作,實(shí)現(xiàn)的功能也差不多,但XPath明顯比re具有優(yōu)勢,在網(wǎng)頁分析上使re退居二線。
XPath介紹
是什么? 全稱為XML Path Language 一種小型的查詢語言
說道XPath是門語言,不得不說它所具備的優(yōu)點(diǎn):
- 可在XML中查找信息
- 支持HTML的查找
- 通過元素和屬性進(jìn)行導(dǎo)航
python開發(fā)使用XPath條件: 由于XPath屬于lxml庫模塊,所以首先要安裝庫lxml。
XPath的簡單調(diào)用方法:
from lxml import etree
selector=etree.HTML(源碼) #將源碼轉(zhuǎn)化為能被XPath匹配的格式
selector.xpath(表達(dá)式) #返回為一列表
2|2Xpath語法
查詢
html_doc = """ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div class="d1"> <div class="d2"> <p class="story"> <a class="sister" id="link1">Elsie</a>, <a class="sister" id="link2">Lacie</a> and <a id="link3">Tillie</a> </p> </div> <div> <p id="p1">ALex is dsb</p> <p id="p2">Egon too</p> </div> </div> <div class="d3"> <a >baidu</a> <p>百度</p> </div> </body> </html> """ from lxml import etree selector=etree.HTML(html_doc) # 將源碼轉(zhuǎn)化為能被XPath匹配的格式 ''' 一、選取節(jié)點(diǎn) nodename 選取nodename節(jié)點(diǎn)的所有子節(jié)點(diǎn) xpath(‘//div’) 選取了所有div節(jié)點(diǎn) / 從根節(jié)點(diǎn)選取 xpath(‘/div’) 從根節(jié)點(diǎn)上選取div節(jié)點(diǎn) // 選取所有的當(dāng)前節(jié)點(diǎn),不考慮他們的位置 xpath(‘//div’) 選取所有的div節(jié)點(diǎn) . 選取當(dāng)前節(jié)點(diǎn) xpath(‘./div’) 選取當(dāng)前節(jié)點(diǎn)下的div節(jié)點(diǎn) .. 選取當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn) xpath(‘..’) 回到上一個(gè)節(jié)點(diǎn) @ 選取屬性 xpath(’//@calss’) 選取所有的class屬性 ''' ret=selector.xpath("//div") ret=selector.xpath("/div") ret=selector.xpath("./div") ret=selector.xpath("//p[@id='p1']") ret=selector.xpath("//div[@class='d1']/div/p[@class='story']") ''' 二、謂語 表達(dá)式 結(jié)果 xpath(‘/body/div[1]’) 選取body下的第一個(gè)div節(jié)點(diǎn) xpath(‘/body/div[last()]’) 選取body下最后一個(gè)div節(jié)點(diǎn) xpath(‘/body/div[last()-1]’) 選取body下倒數(shù)第二個(gè)div節(jié)點(diǎn) xpath(‘/body/div[positon()<3]’) 選取body下前兩個(gè)div節(jié)點(diǎn) xpath(‘/body/div[@class]’) 選取body下帶有class屬性的div節(jié)點(diǎn) xpath(‘/body/div[@class=”main”]’) 選取body下class屬性為main的div節(jié)點(diǎn) xpath(‘/body/div[price>35.00]’) 選取body下price元素值大于35的div節(jié)點(diǎn) ''' ret=selector.xpath("//p[@class='story']//a[2]") ret=selector.xpath("//p[@class='story']//a[last()]") ''' 通配符 Xpath通過通配符來選取未知的XML元素 表達(dá)式 結(jié)果 xpath(’/div/*’) 選取div下的所有子節(jié)點(diǎn) xpath(‘/div[@*]’) 選取所有帶屬性的div節(jié)點(diǎn) ''' ret=selector.xpath("//p[@class='story']/*") ret=selector.xpath("//p[@class='story']/a[@class]") ''' 四、取多個(gè)路徑 使用“|”運(yùn)算符可以選取多個(gè)路徑 表達(dá)式 結(jié)果 xpath(‘//div|//table’) 選取所有的div和table節(jié)點(diǎn) ''' ret=selector.xpath("//p[@class='story']/a[@class]|//div[@class='d3']") print(ret) ''' 五、Xpath軸 軸可以定義相對于當(dāng)前節(jié)點(diǎn)的節(jié)點(diǎn)集 軸名稱 表達(dá)式 描述 ancestor xpath(‘./ancestor::*’) 選取當(dāng)前節(jié)點(diǎn)的所有先輩節(jié)點(diǎn)(父、祖父) ancestor-or-self xpath(‘./ancestor-or-self::*’) 選取當(dāng)前節(jié)點(diǎn)的所有先輩節(jié)點(diǎn)以及節(jié)點(diǎn)本身 attribute xpath(‘./attribute::*’) 選取當(dāng)前節(jié)點(diǎn)的所有屬性 child xpath(‘./child::*’) 返回當(dāng)前節(jié)點(diǎn)的所有子節(jié)點(diǎn) descendant xpath(‘./descendant::*’) 返回當(dāng)前節(jié)點(diǎn)的所有后代節(jié)點(diǎn)(子節(jié)點(diǎn)、孫節(jié)點(diǎn)) following xpath(‘./following::*’) 選取文檔中當(dāng)前節(jié)點(diǎn)結(jié)束標(biāo)簽后的所有節(jié)點(diǎn) following-sibing xpath(‘./following-sibing::*’) 選取當(dāng)前節(jié)點(diǎn)之后的兄弟節(jié)點(diǎn) parent xpath(‘./parent::*’) 選取當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn) preceding xpath(‘./preceding::*’) 選取文檔中當(dāng)前節(jié)點(diǎn)開始標(biāo)簽前的所有節(jié)點(diǎn) preceding-sibling xpath(‘./preceding-sibling::*’) 選取當(dāng)前節(jié)點(diǎn)之前的兄弟節(jié)點(diǎn) self xpath(‘./self::*’) 選取當(dāng)前節(jié)點(diǎn) 六、功能函數(shù) 使用功能函數(shù)能夠更好的進(jìn)行模糊搜索 函數(shù) 用法 解釋 starts-with xpath(‘//div[starts-with(@id,”ma”)]‘) 選取id值以ma開頭的div節(jié)點(diǎn) contains xpath(‘//div[contains(@id,”ma”)]‘) 選取id值包含ma的div節(jié)點(diǎn) and xpath(‘//div[contains(@id,”ma”) and contains(@id,”in”)]‘) 選取id值包含ma和in的div節(jié)點(diǎn) text() xpath(‘//div[contains(text(),”ma”)]‘) 選取節(jié)點(diǎn)文本包含ma的div節(jié)點(diǎn) '''
Element對象
from lxml.etree import _Element for obj in ret: print(obj) print(type(obj)) # from lxml.etree import _Element ''' Element對象 class xml.etree.ElementTree.Element(tag, attrib={}, **extra) tag:string,元素代表的數(shù)據(jù)種類。 text:string,元素的內(nèi)容。 tail:string,元素的尾形。 attrib:dictionary,元素的屬性字典。 #針對屬性的操作 clear():清空元素的后代、屬性、text和tail也設(shè)置為None。 get(key, default=None):獲取key對應(yīng)的屬性值,如該屬性不存在則返回default值。 items():根據(jù)屬性字典返回一個(gè)列表,列表元素為(key, value)。 keys():返回包含所有元素屬性鍵的列表。 set(key, value):設(shè)置新的屬性鍵與值。 #針對后代的操作 append(subelement):添加直系子元素。 extend(subelements):增加一串元素對象作為子元素。#python2.7新特性 find(match):尋找第一個(gè)匹配子元素,匹配對象可以為tag或path。 findall(match):尋找所有匹配子元素,匹配對象可以為tag或path。 findtext(match):尋找第一個(gè)匹配子元素,返回其text值。匹配對象可以為tag或path。 insert(index, element):在指定位置插入子元素。 iter(tag=None):生成遍歷當(dāng)前元素所有后代或者給定tag的后代的迭代器。#python2.7新特性 iterfind(match):根據(jù)tag或path查找所有的后代。 itertext():遍歷所有后代并返回text值。 remove(subelement):刪除子元素。 '''




浙公網(wǎng)安備 33010602011771號