1、介紹
Beautiful Soup 是一個能夠從HTML或XML文件中提取數據的Python庫.它可以經過你喜歡的轉換器實現慣用的文檔導航,查找,修改文檔的方式.Beautiful Soup會幫你節省數小時甚至數天的工做時間,官網推薦在如今的項目中使用Beautiful Soup 4, 移植到BS4,安裝模塊以下:html
# pip3 install beautifulsoup4 #pip3 install LXML
2、用法
python
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> <h1>f</h1> </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') # 找到第一個a標籤 tag2 = soup.find_all(name='a') # 找到全部的a標籤 tag3 = soup.select('#link2') # 找到id=link2的標籤
一、標籤名稱查找
app
tag = soup.find('a') name = tag.name # 獲取標籤名稱,結果a tag.name = 'span' # 設置標籤名稱
二、標籤屬性查找ide
tag = soup.find('a') attrs = tag.attrs # 獲取名稱爲a的標籤的屬性 ,結果{'class': ['sister0'], 'id': 'link1'} tag.attrs = {'ik':123} # 設置 tag.attrs['id'] = 'iiiii' # 設置 # 可使用print(soup)查看設置後的全部標籤屬性
三、子孫標籤查找spa
soup.p.contents #p下全部子節點 soup.p.children #獲得一個迭代器,包含p下全部子節點 for i,child in enumerate(soup.p.children): print(i,child) soup.p.descendants #獲取子孫節點,p下全部的標籤都會選擇出來 for i,child in enumerate(soup.p.descendants): print(i,child)
四、clear,將標籤的全部子標籤所有清空(保留標籤名)code
tag = soup.find('body') tag.clear() # 清空名稱爲body的標籤,保留標籤名
五、decompose,遞歸的刪除全部的標籤orm
body = soup.find('body') body.decompose() # 遞歸的刪除名稱爲body的標籤
六、extract,遞歸的刪除全部的標籤,並獲取刪除的標籤xml
body = soup.find('body') v = body.extract() # 遞歸的刪除名稱爲body的標籤 print(soup) #查看刪除後的結果爲 <html><head><title>The Dormouse's story</title></head> </html>
七、decode,轉換爲字符串(含當前標籤);decode_contents(不含當前標籤)htm
body = soup.find('body') print(body.decode()) #名稱爲body的標籤轉換爲字符串,包含body標籤 print(body.decode_contents()) #名稱爲body的標籤轉換爲字符串,不包含body標籤
八、 encode,轉換爲字節(含當前標籤);encode_contents(不含當前標籤)對象
body = soup.find('body') print(body.encode()) #名稱爲body的標籤轉換爲字節,包含body標籤 print(body.encode_contents()) #名稱爲body的標籤轉換爲字節,不包含body標籤
九、find,獲取匹配的第一個標籤
soup.find_all('title', limit=1) soup.find('title') # 上面兩個查找結果同樣,find查找就至關於find_all查找並設置limit=1 # find_all() 方法的返回結果是值包含一個元素的列表,而 find() 方法直接返回結果. # find_all() 方法沒有找到目標是返回空列表, find() 方法找不到目標時,返回 None soup.find("head").find("title") #能夠簡單寫成 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')
十、find_all,獲取匹配的全部標籤
####### 列表 ####### 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])) #結果:['Tillie'] <class 'bs4.element.NavigableString'> v = soup.find_all(id=['link1','link2']) #查找id爲link1,link2的標籤,結果放在列表裏面 print(v) v = soup.find_all(href=['link1','link2']) #沒找到href爲link1,link2的標籤,因此結果是一個空列表 print(v) ####### 正則 ####### import re rep = re.compile('p') #匹配規則爲p標籤 rep = re.compile('^p') #匹配規則爲p開頭標籤 v = soup.find_all(name=rep) #找p開頭的的全部標籤 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,獲取標籤屬性 tag = soup.find('a') v = tag.get('id') #獲取標籤名稱爲a的id屬性 print(v)
十一、has_attr,檢查標籤是否具備該屬性
tag = soup.find('a') v = tag.has_attr('id') #檢查標籤a是否具備id屬性 print(v)
十二、get_text,獲取標籤內部文本內容
tag = soup.find('a') print(tag) #<a class="sister0" id="link1">Els<span>f</span>ie</a> v = tag.get_text('id') print(v) #結果是:Elsidfidie
1三、index,檢查標籤在某標籤中的索引位置
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)
1四、is_empty_element,是不是空標籤(是否能夠是空)或者自閉合標籤
# 判斷是不是以下標籤:'br', 'hr', 'input', 'img', 'meta', 'spacer', 'link', 'frame', 'base' tag = soup.find('br') v = tag.is_empty_element print(v)
1五、關聯標籤
soup.next soup.next_element #不分層次的查找標籤的下一個節點 soup.next_elements soup.next_sibling #下一個兄弟 soup.next_siblings #下面的兄弟們=>生成器對象 tag.previous tag.previous_element #不分層次的查找標籤的上一個節點 tag.previous_elements tag.previous_sibling #上一個兄弟 tag.previous_siblings #上面的兄弟們=>生成器對象 tag.parent #獲取標籤的父節點 tag.parents #找到標籤全部的祖先節點
1六、查找某標籤的關聯標籤
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(...) # 參數同find_all
1七、select,select_one, CSS選擇器
soup.select("title") #[<title>The Dormouse's story</title>] soup.select("p:nth-of-type(3)") #選擇全部p標籤中的第三個標籤,至關於soup.select(p)[2] soup.select("body a") #body裏的a標籤,結果放在列表顯示 soup.select("html head title") #[<title>The Dormouse's story</title>] soup.select("span,a") #選擇全部的span和a標籤 soup.select("head > title") # 選擇head標籤下的直接title子標籤 soup.select("p > a") # 選擇p標籤下的直接a子標籤 soup.select("p > a:nth-of-type(2)") soup.select("p > #link1") # 選擇p標籤下的直接id爲link1子標籤 soup.select("body > a") # 選擇body標籤下的直接a子標籤 soup.select("#link1 ~ .sister") # 選擇id=link1後的class=sister全部兄弟節點標籤 soup.select("#link1 + .sister") # 選擇id=link1後的class=sister下一個兄弟節點標籤,結果[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>] soup.select(".sister") # 選擇class爲sister的標籤 soup.select("[class~=sister]") # class=sister的全部節點 soup.select("#link1") # 選擇id爲link1的標籤 soup.select("a#link2") # a節點,且id=link2的節點 soup.select('a[href]') # 全部的a節點,有href屬性 soup.select('a[href="http://example.com/elsie"]') # 指定href屬性值的全部a節點 soup.select('a[href^="http://example.com/"]') soup.select('a[href$="tillie"]') # href屬性以指定值結尾的全部a節點 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) #結果:<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) #結果:<class 'list'> [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
1八、標籤的內容
tag = soup.find('span') print(tag.string) # 獲取,結果f tag.string = 'new content' # 設置span標籤內容爲new content tag = soup.find('body') print(tag.string) #結果None tag.string = 'xxx' #<body>xxx</body> tag = soup.find('body') v = tag.stripped_strings # 遞歸內部獲取全部標籤的文本 print(v) #<generator object stripped_strings at 0x00000000021B0360>
1九、append在當前標籤內部追加一個標籤
tag = soup.find('body') tag.append(soup.find('a')) #在body標籤裏追加<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 = '我是一個新來的' tag = soup.find('body') tag.append(obj) #在body標籤裏追加<i id="it">我是一個新來的</i>
20、insert在當前標籤內部指定位置插入一個標籤
from bs4.element import Tag obj = Tag(name='i', attrs={'id': 'it'}) obj.string = '我是一個新來的' tag = soup.find('body') tag.insert(3, obj) print(soup)
2一、insert_after,insert_before 在當前標籤後面或前面插入
from bs4.element import Tag obj = Tag(name='i', attrs={'id': 'it'}) obj.string = '我是一個新來的' tag = soup.find('body') # tag.insert_before(obj) #在當前標籤前面插入 tag.insert_after(obj) #在當前標籤後面插入 print(soup)
2二、replace_with 在當前標籤替換爲指定標籤
from bs4.element import Tag obj = Tag(name='i', attrs={'id': 'it'}) obj.string = '我是一個新來的' tag = soup.find('div') tag.replace_with(obj) print(soup)
2三、建立標籤之間的關係
tag = soup.find('div') a = soup.find('a') tag.setup(previous_sibling=a) print(tag.previous_sibling) #tag的上一個兄弟標籤<a class="sister0" id="link1">Els<span>f</span>ie</a>
2四、wrap,將指定標籤把當前標籤包裹起來
from bs4.element import Tag obj1 = Tag(name='div', attrs={'id': 'it'}) obj1.string = '我是一個新來的' #獲得新標籤<div id="it">我是一個新來的 </div> tag = soup.find('a') v = tag.wrap(obj1) #<div id="it">我是一個新來的<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標籤包住a標籤 <p class="story">...<a class="sister0" id="link1">Els<span>f</span>ie</a></p> print(soup)
2五、unwrap,去掉當前標籤,將保留其包裹的標籤
tag = soup.find('a') v = tag.unwrap() #結果:Els<span>f</span>ie print(soup)