Python下利用BeautifulSoup解析HTML的實(shí)現(xiàn)
摘要
Beautiful Soup 是一個(gè)可以從 HTML 或 XML 格式文件中提取數(shù)據(jù)的 Python 庫(kù),他可以將HTML 或 XML 數(shù)據(jù)解析為Python 對(duì)象,以方便通過(guò)Python代碼進(jìn)行處理。
文檔環(huán)境
- Centos7.5
- Python2.7
- BeautifulSoup4
Beautifu Soup 使用說(shuō)明
Beautiful Soup 的基本功能就是對(duì)HTML的標(biāo)簽進(jìn)行查找及編輯。
基本概念-對(duì)象類(lèi)型
Beautiful Soup 將復(fù)雜 HTML 文檔轉(zhuǎn)換成一個(gè)復(fù)雜的樹(shù)形結(jié)構(gòu),每個(gè)節(jié)點(diǎn)都被轉(zhuǎn)換成一個(gè)Python 對(duì)象,Beautiful Soup將這些對(duì)象定義了4 種類(lèi)型: Tag、NavigableString、BeautifulSoup、Comment 。
對(duì)象類(lèi)型 | 描述 |
---|---|
BeautifulSoup | 文檔的全部?jī)?nèi)容 |
Tag | HTML的標(biāo)簽 |
NavigableString | 標(biāo)簽包含的文字 |
Comment | 是一種特殊的NavigableString類(lèi)型,當(dāng)標(biāo)簽中的NavigableString 被注釋時(shí),則定義為該類(lèi)型 |
安裝及引用
# Beautiful Soup pip install bs4 # 解析器 pip install lxml pip install html5lib
# 初始化 from bs4 import BeautifulSoup # 方法一,直接打開(kāi)文件 soup = BeautifulSoup(open("index.html")) # 方法二,指定數(shù)據(jù) resp = "<html>data</html>" soup = BeautifulSoup(resp, 'lxml') # soup 為 BeautifulSoup 類(lèi)型對(duì)象 print(type(soup))
標(biāo)簽搜索及過(guò)濾
基本方法
標(biāo)簽搜索有find_all() 和find() 兩個(gè)基本的搜索方法,find_all() 方法會(huì)返回所有匹配關(guān)鍵字的標(biāo)簽列表,find()方法則只返回一個(gè)匹配結(jié)果。
soup = BeautifulSoup(resp, 'lxml') # 返回一個(gè)標(biāo)簽名為"a"的Tag soup.find("a") # 返回所有tag 列表 soup.find_all("a") ## find_all方法可被簡(jiǎn)寫(xiě) soup("a") #找出所有以b開(kāi)頭的標(biāo)簽 for tag in soup.find_all(re.compile("^b")): print(tag.name) #找出列表中的所有標(biāo)簽 soup.find_all(["a", "p"]) # 查找標(biāo)簽名為p,class屬性為"title" soup.find_all("p", "title") # 查找屬性id為"link2" soup.find_all(id="link2") # 查找存在屬性id的 soup.find_all(id=True) # soup.find_all(href=re.compile("elsie"), id='link1') # soup.find_all(attrs={"data-foo": "value"}) #查找標(biāo)簽文字包含"sisters" soup.find(string=re.compile("sisters")) # 獲取指定數(shù)量的結(jié)果 soup.find_all("a", limit=2) # 自定義匹配方法 def has_class_but_no_id(tag): return tag.has_attr('class') and not tag.has_attr('id') soup.find_all(has_class_but_no_id) # 僅對(duì)屬性使用自定義匹配方法 def not_lacie(href): return href and not re.compile("lacie").search(href) soup.find_all(href=not_lacie) # 調(diào)用tag的 find_all() 方法時(shí),Beautiful Soup會(huì)檢索當(dāng)前tag的所有子孫節(jié)點(diǎn),如果只想搜索tag的直接子節(jié)點(diǎn),可以使用參數(shù) recursive=False soup.find_all("title", recursive=False)
擴(kuò)展方法
ind_parents() | 所有父輩節(jié)點(diǎn) |
find_parent() | 第一個(gè)父輩節(jié)點(diǎn) |
find_next_siblings() | 之后的所有兄弟節(jié)點(diǎn) |
find_next_sibling() | 之后的第一個(gè)兄弟節(jié)點(diǎn) |
find_previous_siblings() | 之前的所有兄弟節(jié)點(diǎn) |
find_previous_sibling() | 之前的第一個(gè)兄弟節(jié)點(diǎn) |
find_all_next() | 之后的所有元素 |
find_next() | 之后的第一個(gè)元素 |
find_all_previous() | 之前的所有元素 |
find_previous() | 之前的第一個(gè)元素 |
CSS選擇器
Beautiful Soup支持大部分的CSS選擇器 http://www.w3.org/TR/CSS2/selector.html, 在 Tag 或 BeautifulSoup 對(duì)象的 .select() 方法中傳入字符串參數(shù), 即可使用CSS選擇器的語(yǔ)法找到tag。
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 rel="external nofollow" class="sister" id="link1">Elsie</a>, <a rel="external nofollow" class="sister" id="link2">Lacie</a> and <a rel="external nofollow" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well. </p> <p class="story">...</p> """ soup = BeautifulSoup(html_doc) # 所有 a 標(biāo)簽 soup.select("a") # 逐層查找 soup.select("body a") soup.select("html head title") # tag標(biāo)簽下的直接子標(biāo)簽 soup.select("head > title") soup.select("p > #link1") # 所有匹配標(biāo)簽之后的兄弟標(biāo)簽 soup.select("#link1 ~ .sister") # 匹配標(biāo)簽之后的第一個(gè)兄弟標(biāo)簽 soup.select("#link1 + .sister") # 根據(jù)calss類(lèi)名 soup.select(".sister") soup.select("[class~=sister]") # 根據(jù)ID查找 soup.select("#link1") soup.select("a#link1") # 根據(jù)多個(gè)ID查找 soup.select("#link1,#link2") # 根據(jù)屬性查找 soup.select('a[href]') # 根據(jù)屬性值查找 soup.select('a[href^="http://example.com/"]') soup.select('a[href$="tillie"]') soup.select('a[href*=".com/el"]') # 只獲取一個(gè)匹配結(jié)果 soup.select(".sister", limit=1) # 只獲取一個(gè)匹配結(jié)果 soup.select_one(".sister")
標(biāo)簽對(duì)象方法
標(biāo)簽屬性
soup = BeautifulSoup('<p class="body strikeout" id="1">Extremely bold</p><p class="body strikeout" id="2">Extremely bold2</p>') # 獲取所有的 p標(biāo)簽對(duì)象 tags = soup.find_all("p") # 獲取第一個(gè)p標(biāo)簽對(duì)象 tag = soup.p # 輸出標(biāo)簽類(lèi)型 type(tag) # 標(biāo)簽名 tag.name # 標(biāo)簽屬性 tag.attrs # 標(biāo)簽屬性class 的值 tag['class'] # 標(biāo)簽包含的文字內(nèi)容,對(duì)象NavigableString 的內(nèi)容 tag.string # 返回標(biāo)簽內(nèi)所有的文字內(nèi)容 for string in tag.strings: print(repr(string)) # 返回標(biāo)簽內(nèi)所有的文字內(nèi)容, 并去掉空行 for string in tag.stripped_strings: print(repr(string)) # 獲取到tag中包含的所有及包括子孫tag中的NavigableString內(nèi)容,并以Unicode字符串格式輸出 tag.get_text() ## 以"|"分隔 tag.get_text("|") ## 以"|"分隔,不輸出空字符 tag.get_text("|", strip=True) 獲取子節(jié)點(diǎn) tag.contents # 返回第一層子節(jié)點(diǎn)的列表 tag.children # 返回第一層子節(jié)點(diǎn)的listiterator 對(duì)象 for child in tag.children: print(child) tag.descendants # 遞歸返回所有子節(jié)點(diǎn) for child in tag.descendants: print(child)
獲取父節(jié)點(diǎn)
tag.parent # 返回第一層父節(jié)點(diǎn)標(biāo)簽 tag.parents # 遞歸得到元素的所有父輩節(jié)點(diǎn) for parent in tag.parents: if parent is None: print(parent) else: print(parent.name)
獲取兄弟節(jié)點(diǎn)
# 下一個(gè)兄弟元素 tag.next_sibling # 當(dāng)前標(biāo)簽之后的所有兄弟元素 tag.next_siblings for sibling in tag.next_siblings: print(repr(sibling)) # 上一個(gè)兄弟元素 tag.previous_sibling # 當(dāng)前標(biāo)簽之前的所有兄弟元素 tag.previous_siblings for sibling in tag.previous_siblings: print(repr(sibling))
元素的遍歷
Beautiful Soup中把每個(gè)tag定義為一個(gè)“element”,每個(gè)“element”,被自上而下的在HTML中排列,可以通過(guò)遍歷命令逐個(gè)顯示標(biāo)簽
# 當(dāng)前標(biāo)簽的下一個(gè)元素 tag.next_element # 當(dāng)前標(biāo)簽之后的所有元素 for element in tag.next_elements: print(repr(element)) # 當(dāng)前標(biāo)簽的前一個(gè)元素 tag.previous_element # 當(dāng)前標(biāo)簽之前的所有元素 for element in tag.previous_elements: print(repr(element))
修改標(biāo)簽屬性
soup = BeautifulSoup('<b class="boldest">Extremely bold</b>') tag = soup.b tag.name = "blockquote" tag['class'] = 'verybold' tag['id'] = 1 tag.string = "New link text." print(tag)
修改標(biāo)簽內(nèi)容(NavigableString)
soup = BeautifulSoup('<b class="boldest">Extremely bold</b>') tag = soup.b tag.string = "New link text."
添加標(biāo)簽內(nèi)容(NavigableString)
soup = BeautifulSoup("<a>Foo</a>") tag = soup.a tag.append("Bar") tag.contents # 或者 new_string = NavigableString("Bar") tag.append(new_string) print(tag)
添加注釋(Comment)
注釋是一個(gè)特殊的NavigableString 對(duì)象,所以同樣可以通過(guò)append() 方法進(jìn)行添加。
from bs4 import Comment soup = BeautifulSoup("<a>Foo</a>") new_comment = soup.new_string("Nice to see you.", Comment) tag.append(new_comment) print(tag)
添加標(biāo)簽(Tag)
添加標(biāo)簽方法有兩種,一種是在指定標(biāo)簽的內(nèi)部添加(append方法),另一種是在指定位置添加(insert、insert_before、insert_after方法)
append方法
soup = BeautifulSoup("<b></b>") tag = soup.b new_tag = soup.new_tag("a", rel="external nofollow" ) new_tag.string = "Link text." tag.append(new_tag) print(tag)
* insert方法,是指在當(dāng)前標(biāo)簽子節(jié)點(diǎn)列表的指定位置插入對(duì)象(Tag或NavigableString)
html = '<b><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >I linked to <i>example.com</i></a></b>' soup = BeautifulSoup(html) tag = soup.a tag.contents tag.insert(1, "but did not endorse ") tag.contents
insert_before() 和 insert_after() 方法則在當(dāng)前標(biāo)簽之前或之后的兄弟節(jié)點(diǎn)添加元素
html = '<b><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >I linked to <i>example.com</i></a></b>' soup = BeautifulSoup(html) tag = soup.new_tag("i") tag.string = "Don't" soup.b.insert_before(tag) soup.b
* wrap() 和 unwrap()可以對(duì)指定的tag元素進(jìn)行包裝或解包,并返回包裝后的結(jié)果。
```python # 添加包裝 soup = BeautifulSoup("<p>I wish I was bold.</p>") soup.p.string.wrap(soup.new_tag("b")) #輸出 <b>I wish I was bold.</b> soup.p.wrap(soup.new_tag("div")) #輸出 <div><p><b>I wish I was bold.</b></p></div> # 拆解包裝 markup = '<a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >I linked to <i>example.com</i></a>' soup = BeautifulSoup(markup) a_tag = soup.a a_tag.i.unwrap() a_tag #輸出 <a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >I linked to example.com</a>
刪除標(biāo)簽
html = '<b><a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >I linked to <i>example.com</i></a></b>' soup = BeautifulSoup(html) # 清楚當(dāng)前標(biāo)簽的所有子節(jié)點(diǎn) soup.b.clear() # 將當(dāng)前標(biāo)簽及所有子節(jié)點(diǎn)從soup 中移除,返回當(dāng)前標(biāo)簽。 b_tag=soup.b.extract() b_tag soup # 將當(dāng)前標(biāo)簽及所有子節(jié)點(diǎn)從soup 中移除,無(wú)返回。 soup.b.decompose() # 將當(dāng)前標(biāo)簽替換為指定的元素 tag=soup.i new_tag = soup.new_tag("p") new_tag.string = "Don't" tag.replace_with(new_tag)
其他方法
輸出
# 格式化輸出 tag.prettify() tag.prettify("latin-1")
- 使用Beautiful Soup解析后,文檔都被轉(zhuǎn)換成了Unicode,特殊字符也被轉(zhuǎn)換為Unicode,如果將文檔轉(zhuǎn)換成字符串,Unicode編碼會(huì)被編碼成UTF-8.這樣就無(wú)法正確顯示HTML特殊字符了
- 使用Unicode時(shí),Beautiful Soup還會(huì)智能的把“引號(hào)”轉(zhuǎn)換成HTML或XML中的特殊字符
文檔編碼
使用Beautiful Soup解析后,文檔都被轉(zhuǎn)換成了Unicode,其使用了“編碼自動(dòng)檢測(cè)”子庫(kù)來(lái)識(shí)別當(dāng)前文檔編碼并轉(zhuǎn)換成Unicode編碼。
soup = BeautifulSoup(html) soup.original_encoding # 也可以手動(dòng)指定文檔的編碼 soup = BeautifulSoup(html, from_encoding="iso-8859-8") soup.original_encoding # 為提高“編碼自動(dòng)檢測(cè)”的檢測(cè)效率,也可以預(yù)先排除一些編碼 soup = BeautifulSoup(markup, exclude_encodings=["ISO-8859-7"]) 通過(guò)Beautiful Soup輸出文檔時(shí),不管輸入文檔是什么編碼方式,默認(rèn)輸出編碼均為UTF-8編碼 文檔解析器 Beautiful Soup目前支持, “l(fā)xml”, “html5lib”, 和 “html.parser” soup=BeautifulSoup("<a><b /></a>") soup #輸出: <html><body><a><b></b></a></body></html> soup=BeautifulSoup("<a></p>", "lxml") soup #輸出: <html><body><a></a></body></html> soup=BeautifulSoup("<a></p>", "html5lib") soup #輸出: <html><head></head><body><a><p></p></a></body></html> soup=BeautifulSoup("<a></p>", "html.parser") soup #輸出: <a></a>
參考文檔
https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python之yield表達(dá)式學(xué)習(xí)
這篇文章主要介紹了python之yield表達(dá)式學(xué)習(xí),python中有一個(gè)略微奇怪的表達(dá)式叫yield expression,本文就來(lái)探究一下這是個(gè)什么東西,需要的朋友可以參考下2014-09-09Python實(shí)現(xiàn)aes加密解密多種方法解析
這篇文章主要介紹了Python實(shí)現(xiàn)aes加密解密多種方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05pandas按條件篩選數(shù)據(jù)的實(shí)現(xiàn)
這篇文章主要介紹了pandas按條件篩選數(shù)據(jù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02詳談python中冒號(hào)與逗號(hào)的區(qū)別
下面小編就為大家分享一篇詳談python中冒號(hào)與逗號(hào)的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04python實(shí)現(xiàn)按任意鍵繼續(xù)執(zhí)行程序
本文給大家分享的是如何使用Python腳本實(shí)現(xiàn)按任意鍵繼續(xù)執(zhí)行程序的代碼,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下2016-12-12Python函數(shù)關(guān)鍵字參數(shù)及用法詳解
本文主要介紹了Python函數(shù)關(guān)鍵字參數(shù)及用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03