一、介紹
Beautiful Soup 是一個(gè)可以從HTML或XML文件中提取數(shù)據(jù)的Python庫(kù).它能夠通過(guò)你喜歡的轉(zhuǎn)換器實(shí)現(xiàn)慣用的文檔導(dǎo)航,查找,修改文檔的方式.Beautiful Soup會(huì)幫你節(jié)省數(shù)小時(shí)甚至數(shù)天的工作時(shí)間,官網(wǎng)推薦在現(xiàn)在的項(xiàng)目中使用Beautiful Soup 4, 移植到BS4,安裝模塊如下:
# pip3 install beautifulsoup4 #pip3 install LXML
二、用法
from bs4 import BeautifulSoup html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> asdf <div class="title"> <b>The Dormouse's story總共</b> <h2>f</h2> </div> <div class="story">Once upon a time there were three little sisters; and their names were <a class="sister0" id="link1">Els<span>f</span>ie</a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</div> ad<br/>sf <p class="story">...</p> </body> </html> """ soup=BeautifulSoup(html_doc, features="lxml") tag1 = soup.find(name='a') # 找到第一個(gè)a標(biāo)簽 tag2 = soup.find_all(name='a') # 找到所有的a標(biāo)簽 tag3 = soup.select('#link2') # 找到id=link2的標(biāo)簽
1、標(biāo)簽名稱(chēng)查找
tag = soup.find('a') name = tag.name # 獲取標(biāo)簽名稱(chēng),結(jié)果a tag.name = 'span' # 設(shè)置標(biāo)簽名稱(chēng)
2、標(biāo)簽屬性查找
tag = soup.find('a') attrs = tag.attrs # 獲取名稱(chēng)為a的標(biāo)簽的屬性 ,結(jié)果{'class': ['sister0'], 'id': 'link1'} tag.attrs = {'ik':123} # 設(shè)置 tag.attrs['id'] = 'iiiii' # 設(shè)置 # 可以使用print(soup)查看設(shè)置后的所有標(biāo)簽屬性
3、子孫標(biāo)簽查找
soup.p.contents #p下所有子節(jié)點(diǎn) soup.p.children #得到一個(gè)迭代器,包含p下所有子節(jié)點(diǎn) for i,child in enumerate(soup.p.children): print(i,child) soup.p.descendants #獲取子孫節(jié)點(diǎn),p下所有的標(biāo)簽都會(huì)選擇出來(lái) for i,child in enumerate(soup.p.descendants): print(i,child)
4、clear,將標(biāo)簽的所有子標(biāo)簽全部清空(保留標(biāo)簽名)
tag = soup.find('body') tag.clear() # 清空名稱(chēng)為body的標(biāo)簽,保留標(biāo)簽名
5、decompose,遞歸的刪除所有的標(biāo)簽
body = soup.find('body') body.decompose() # 遞歸的刪除名稱(chēng)為body的標(biāo)簽
6、extract,遞歸的刪除所有的標(biāo)簽,并獲取刪除的標(biāo)簽
body = soup.find('body') v = body.extract() # 遞歸的刪除名稱(chēng)為body的標(biāo)簽 print(soup) #查看刪除后的結(jié)果為 <html><head><title>The Dormouse's story</title></head> </html>
7、decode,轉(zhuǎn)換為字符串(含當(dāng)前標(biāo)簽);decode_contents(不含當(dāng)前標(biāo)簽)
body = soup.find('body') print(body.decode()) #名稱(chēng)為body的標(biāo)簽轉(zhuǎn)換為字符串,包含body標(biāo)簽 print(body.decode_contents()) #名稱(chēng)為body的標(biāo)簽轉(zhuǎn)換為字符串,不包含body標(biāo)簽
8、 encode,轉(zhuǎn)換為字節(jié)(含當(dāng)前標(biāo)簽);encode_contents(不含當(dāng)前標(biāo)簽)
body = soup.find('body') print(body.encode()) #名稱(chēng)為body的標(biāo)簽轉(zhuǎn)換為字節(jié),包含body標(biāo)簽 print(body.encode_contents()) #名稱(chēng)為body的標(biāo)簽轉(zhuǎn)換為字節(jié),不包含body標(biāo)簽
9、find,獲取匹配的第一個(gè)標(biāo)簽
soup.find_all('title', limit=1) soup.find('title') # 上面兩個(gè)查找結(jié)果一樣,find查找就相當(dāng)于find_all查找并設(shè)置limit=1 # find_all() 方法的返回結(jié)果是值包含一個(gè)元素的列表,而 find() 方法直接返回結(jié)果. # find_all() 方法沒(méi)有找到目標(biāo)是返回空列表, find() 方法找不到目標(biāo)時(shí),返回 None soup.find("head").find("title") #可以簡(jiǎn)單寫(xiě)成 soup.head.title tag = soup.find(name='a', attrs={'class': 'sister'}, recursive=True, text='Lacie') tag = soup.find(name='a', class_='sister', recursive=True, text='Lacie')
10、find_all,獲取匹配的所有標(biāo)簽
####### 列表 ####### v = soup.find_all(name=['a','div']) print(v) v = soup.find_all(class_=['sister0', 'sister']) print(v) v = soup.find_all(text=['Tillie']) print(v, type(v[0])) #結(jié)果:['Tillie'] <class 'bs4.element.NavigableString'> v = soup.find_all(id=['link1','link2']) #查找id為link1,link2的標(biāo)簽,結(jié)果放在列表里面 print(v) v = soup.find_all(href=['link1','link2']) #沒(méi)找到href為link1,link2的標(biāo)簽,所以結(jié)果是一個(gè)空列表 print(v) ####### 正則 ####### import re rep = re.compile('p') #匹配規(guī)則為p標(biāo)簽 rep = re.compile('^p') #匹配規(guī)則為p開(kāi)頭標(biāo)簽 v = soup.find_all(name=rep) #找p開(kāi)頭的的所有標(biāo)簽 rep = re.compile('sister.*') v = soup.find_all(class_=rep) v = soup.find_all(attrs={"class":rep}) #和上面的一樣 rep = re.compile('http://www.oldboy.com/static/.*') v = soup.find_all(href=rep) #匹配 ####### 方法篩選 ####### def func(tag): return tag.has_attr('class') and tag.has_attr('id') v = soup.find_all(name=func) print(v) get,獲取標(biāo)簽屬性 tag = soup.find('a') v = tag.get('id') #獲取標(biāo)簽名稱(chēng)為a的id屬性 print(v)
11、has_attr,檢查標(biāo)簽是否具有該屬性
tag = soup.find('a') v = tag.has_attr('id') #檢查標(biāo)簽a是否具有id屬性 print(v)
12、get_text,獲取標(biāo)簽內(nèi)部文本內(nèi)容
tag = soup.find('a') print(tag) #<a class="sister0" id="link1">Els<span>f</span>ie</a> v = tag.get_text('id') print(v) #結(jié)果是:Elsidfidie
13、index,檢查標(biāo)簽在某標(biāo)簽中的索引位置
tag = soup.find('body') v = tag.index(tag.find('div')) print(v) tag = soup.find('body') for i,v in enumerate(tag): print(i,v)
14、is_empty_element,是否是空標(biāo)簽(是否可以是空)或者自閉合標(biāo)簽
# 判斷是否是如下標(biāo)簽:'br', 'hr', 'input', 'img', 'meta', 'spacer', 'link', 'frame', 'base' tag = soup.find('br') v = tag.is_empty_element print(v)
15、關(guān)聯(lián)標(biāo)簽
soup.next soup.next_element #不分層次的查找標(biāo)簽的下一個(gè)節(jié)點(diǎn) soup.next_elements soup.next_sibling #下一個(gè)兄弟 soup.next_siblings #下面的兄弟們=>生成器對(duì)象 tag.previous tag.previous_element #不分層次的查找標(biāo)簽的上一個(gè)節(jié)點(diǎn) tag.previous_elements tag.previous_sibling #上一個(gè)兄弟 tag.previous_siblings #上面的兄弟們=>生成器對(duì)象 tag.parent #獲取標(biāo)簽的父節(jié)點(diǎn) tag.parents #找到標(biāo)簽所有的祖先節(jié)點(diǎn)
16、查找某標(biāo)簽的關(guān)聯(lián)標(biāo)簽
tag.find_next(...) tag.find_all_next(...) tag.find_next_sibling(...) tag.find_next_siblings(...) tag.find_previous(...) tag.find_all_previous(...) tag.find_previous_sibling(...) tag.find_previous_siblings(...) tag.find_parent(...) tag.find_parents(...) # 參數(shù)同find_all
17、select,select_one, CSS選擇器
soup.select("title") #[<title>The Dormouse's story</title>] soup.select("p:nth-of-type(3)") #選擇所有p標(biāo)簽中的第三個(gè)標(biāo)簽,相當(dāng)于soup.select(p)[2] soup.select("body a") #body里的a標(biāo)簽,結(jié)果放在列表顯示 soup.select("html head title") #[<title>The Dormouse's story</title>] soup.select("span,a") #選擇所有的span和a標(biāo)簽 soup.select("head > title") # 選擇head標(biāo)簽下的直接title子標(biāo)簽 soup.select("p > a") # 選擇p標(biāo)簽下的直接a子標(biāo)簽 soup.select("p > a:nth-of-type(2)") soup.select("p > #link1") # 選擇p標(biāo)簽下的直接id為link1子標(biāo)簽 soup.select("body > a") # 選擇body標(biāo)簽下的直接a子標(biāo)簽 soup.select("#link1 ~ .sister") # 選擇id=link1后的class=sister所有兄弟節(jié)點(diǎn)標(biāo)簽 soup.select("#link1 + .sister") # 選擇id=link1后的class=sister下一個(gè)兄弟節(jié)點(diǎn)標(biāo)簽,結(jié)果[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>] soup.select(".sister") # 選擇class為sister的標(biāo)簽 soup.select("[class~=sister]") # class=sister的所有節(jié)點(diǎn) soup.select("#link1") # 選擇id為link1的標(biāo)簽 soup.select("a#link2") # a節(jié)點(diǎn),且id=link2的節(jié)點(diǎn) soup.select('a[href]') # 所有的a節(jié)點(diǎn),有href屬性 soup.select('a[href="http://example.com/elsie"]') # 指定href屬性值的所有a節(jié)點(diǎn) soup.select('a[href^="http://example.com/"]') soup.select('a[href$="tillie"]') # href屬性以指定值結(jié)尾的所有a節(jié)點(diǎn) soup.select('a[href*=".com/el"]') from bs4.element import Tag def default_candidate_generator(tag): for child in tag.descendants: if not isinstance(child, Tag): continue if not child.has_attr('href'): continue yield child tags = soup.find('body').select("a", _candidate_generator=default_candidate_generator) print(type(tags), tags) #結(jié)果:<class 'list'> [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] from bs4.element import Tag def default_candidate_generator(tag): for child in tag.descendants: if not isinstance(child, Tag): continue if not child.has_attr('href'): continue yield child tags = soup.find('body').select("a", _candidate_generator=default_candidate_generator, limit=1) print(type(tags), tags) #結(jié)果:<class 'list'> [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
18、標(biāo)簽的內(nèi)容
tag = soup.find('span') print(tag.string) # 獲取,結(jié)果f tag.string = 'new content' # 設(shè)置span標(biāo)簽內(nèi)容為new content tag = soup.find('body') print(tag.string) #結(jié)果None tag.string = 'xxx' #<body>xxx</body> tag = soup.find('body') v = tag.stripped_strings # 遞歸內(nèi)部獲取所有標(biāo)簽的文本 print(v) #<generator object stripped_strings at 0x00000000021B0360>
19、append在當(dāng)前標(biāo)簽內(nèi)部追加一個(gè)標(biāo)簽
tag = soup.find('body') tag.append(soup.find('a')) #在body標(biāo)簽里追加<a class="sister0" id="link1">Els<span>f</span>ie</a> from bs4.element import Tag obj = Tag(name='i',attrs={'id': 'it'}) obj.string = '我是一個(gè)新來(lái)的' tag = soup.find('body') tag.append(obj) #在body標(biāo)簽里追加<i id="it">我是一個(gè)新來(lái)的</i>
20、insert在當(dāng)前標(biāo)簽內(nèi)部指定位置插入一個(gè)標(biāo)簽
from bs4.element import Tag obj = Tag(name='i', attrs={'id': 'it'}) obj.string = '我是一個(gè)新來(lái)的' tag = soup.find('body') tag.insert(3, obj) print(soup)
21、insert_after,insert_before 在當(dāng)前標(biāo)簽后面或前面插入
from bs4.element import Tag obj = Tag(name='i', attrs={'id': 'it'}) obj.string = '我是一個(gè)新來(lái)的' tag = soup.find('body') # tag.insert_before(obj) #在當(dāng)前標(biāo)簽前面插入 tag.insert_after(obj) #在當(dāng)前標(biāo)簽后面插入 print(soup)
22、replace_with 在當(dāng)前標(biāo)簽替換為指定標(biāo)簽
from bs4.element import Tag obj = Tag(name='i', attrs={'id': 'it'}) obj.string = '我是一個(gè)新來(lái)的' tag = soup.find('div') tag.replace_with(obj) print(soup)
23、創(chuàng)建標(biāo)簽之間的關(guān)系
tag = soup.find('div') a = soup.find('a') tag.setup(previous_sibling=a) print(tag.previous_sibling) #tag的上一個(gè)兄弟標(biāo)簽<a class="sister0" id="link1">Els<span>f</span>ie</a>
24、wrap,將指定標(biāo)簽把當(dāng)前標(biāo)簽包裹起來(lái)
from bs4.element import Tag obj1 = Tag(name='div', attrs={'id': 'it'}) obj1.string = '我是一個(gè)新來(lái)的' #得到新標(biāo)簽<div id="it">我是一個(gè)新來(lái)的 </div> tag = soup.find('a') v = tag.wrap(obj1) #<div id="it">我是一個(gè)新來(lái)的<a class="sister0" id="link1">Els<span>f</span>ie</a></div> print(soup) tag = soup.find('a') v = tag.wrap(soup.find('p')) #p標(biāo)簽包住a標(biāo)簽 <p class="story">...<a class="sister0" id="link1">Els<span>f</span>ie</a></p> print(soup)
25、unwrap,去掉當(dāng)前標(biāo)簽,將保留其包裹的標(biāo)簽
tag = soup.find('a') v = tag.unwrap() #結(jié)果:Els<span>f</span>ie print(soup)
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
新聞標(biāo)題:爬蟲(chóng)之beautifulsoup模塊-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://m.2m8n56k.cn/article40/dhpsho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站營(yíng)銷(xiāo)、關(guān)鍵詞優(yōu)化、網(wǎng)站設(shè)計(jì)公司、品牌網(wǎng)站制作、App設(shè)計(jì)
聲明:本網(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;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容