Python數(shù)據(jù)解析之BeautifulSoup4的用法詳解
BeautifulSoup 是什么
Beautiful Soup 是一個可以從 HTML 或 XML 文件中提取數(shù)據(jù)的 Python 庫。,最主要的功能是從網(wǎng)頁抓取數(shù)據(jù)。能夠通過自己喜歡的轉(zhuǎn)換器實現(xiàn)慣用的文檔導(dǎo)航,查找,修改文檔的方式。
注:BeautifulSoup3目前已經(jīng)停止開發(fā),官網(wǎng)推薦在現(xiàn)在的項目中使用BeautifulSoup4
bs4的安裝
可以在 Pycharm 中,輸入以下語句:然后可根據(jù)提示進行安裝。
from bs4 import BeautifulSoup
注意:bs4 是依賴 lxml 庫的,只有先安裝 lxml 庫才可以安裝bs4庫*
文檔解析器優(yōu)缺點
推薦使用 lxml 作為解析器,因為效率更高。
bs4 的使用
- 導(dǎo)入解析包;
- 創(chuàng)建 beautifulsoup 解析對象;
- 打印對應(yīng)內(nèi)容即可;
代碼實例:
from bs4 import BeautifulSoup 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> """ # 創(chuàng)建一個 soup 對象 soup = BeautifulSoup(html_doc, 'lxml') print(soup, type(soup)) # <class 'bs4.BeautifulSoup'> # 格式化文檔輸出 print(soup.prettify()) # 獲取 title 標簽的名稱 title print(soup.title.name) # title # 獲取 title 標簽內(nèi)容 print(soup.title) # <title>The Dormouse's story</title> # title 標簽里面的文本內(nèi)容 print(soup.title.string) # 獲取 p 段落 print(soup.p)
bs4的對象種類
- tag : html中的標簽??梢酝ㄟ^ BeautifulSoup 分析 Tag 的具體內(nèi)容,具體格式為:soup.name,其中 name 是html 下的標簽。
- NavigableString : 標簽中的文本對象。
- BeautifulSoup : 整個html文本對象,可以作為Tag對象。
- Comment:特殊的 NavigableString 對象,如果 html標簽中有注釋,則可過濾注釋符號并保留注釋文本。
代碼實例
from bs4 import BeautifulSoup 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> """ """ tag:標簽 NavigableString:可導(dǎo)航的字符串,標簽中的文本對象 beautifulSoup:bs對象,整個 html 文本對象 Comment:注釋,如果 html 標簽中有注釋,則可過濾注釋符號并保留注釋文本 """ # html_doc 表示要解析的文檔,而 html.parser 表示解析文檔時所用的解析器 soup = BeautifulSoup(html_doc, 'html.parser') print(soup) """ tag:標簽""" print(type(soup.title)) print(type(soup.p)) print(type(soup.a)) """ NavigableString,可導(dǎo)航的字符串""" from bs4.element import NavigableString print(type(soup.title.string)) # 標簽下的文本數(shù)據(jù) """beautifulSoup,bs對象""" print(type(soup)) """ Comment:注釋""" html = "<b><!--好好學(xué)習(xí),天天向上--></b>" soup2 = BeautifulSoup(html, 'html.parser') print(soup2.b.string, type(soup2.b.string))
遍歷文檔樹
遍歷子節(jié)點
- contents 返回的是一個所有子節(jié)點的列表(了解)
- children 返回的是一個子節(jié)點的迭代器(了解)
- descendants 返回的是一個生成器遍歷子子孫孫(了解)
- string 獲取標簽里面的內(nèi)容(掌握)
- strings 返回是一個生成器對象用過來獲取多個標簽內(nèi)容(掌握)
- stripped_strings 和strings 基本一致 但是它可以把多余的空格去掉(掌握)
遍歷父節(jié)點
- parent 直接獲得父節(jié)點
- parents 獲取所有的父節(jié)點
遍歷兄弟節(jié)點
- next_sibling,下一個兄弟結(jié)點
- previous_sibling,上一個兄弟結(jié)點
- next_siblings,下一個所有兄弟結(jié)點
- previous_siblings,上一個所有兄弟結(jié)點
代碼實例
from bs4 import BeautifulSoup 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> </body> </html> """ soup = BeautifulSoup(html_doc, 'lxml') r1 = soup.title.string # 獲取單個標簽里面的內(nèi)容 The Dormouse's story # 獲取html中所有的標簽中的內(nèi)容 r2 = soup.html.strings # 返回是一個生成 generator對象,用過來獲取多個標簽內(nèi)容 for i in r2: print(i) r3 = soup.html.stripped_strings # 獲取html中所有的標簽中的內(nèi)容,并去掉多余的空格 for i in r3: print("---", i)
搜索文檔樹
- find():返回搜索到的第一條數(shù)據(jù);
- find_all():以列表形式返回所有的搜索到的標簽數(shù)據(jù);
代碼實例
from bs4 import BeautifulSoup html = """ <table class="tablelist" cellpadding="0" cellspacing="0"> <tbody> <tr class="h"> <td class="l" width="374">職位名稱</td> <td>職位類別</td> <td>人數(shù)</td> <td>地點</td> <td>發(fā)布時間</td> </tr> <tr class="even"> <td class="l square"><a target="_blank" href="position_detail.php?id=33824&keywords=python&tid=87&lid=2218">22989-金融云區(qū)塊鏈高級研發(fā)工程師(深圳)</a></td> <td>技術(shù)類</td> <td>1</td> <td>深圳</td> <td>2017-11-25</td> </tr> <tr class="odd"> <td class="l square"><a target="_blank" href="position_detail.php?id=29938&keywords=python&tid=87&lid=2218">22989-金融云高級后臺開發(fā)</a></td> <td>技術(shù)類</td> <td>2</td> <td>深圳</td> <td>2017-11-25</td> </tr> <tr class="even"> <td class="l square"><a target="_blank" href="position_detail.php?id=31236&keywords=python&tid=87&lid=2218">SNG16-騰訊音樂運營開發(fā)工程師(深圳)</a></td> <td>技術(shù)類</td> <td>2</td> <td>深圳</td> <td>2017-11-25</td> </tr> <tr class="odd"> <td class="l square"><a target="_blank" href="position_detail.php?id=31235&keywords=python&tid=87&lid=2218">SNG16-騰訊音樂業(yè)務(wù)運維工程師(深圳)</a></td> <td>技術(shù)類</td> <td>1</td> <td>深圳</td> <td>2017-11-25</td> </tr> <tr class="even"> <td class="l square"><a target="_blank" href="position_detail.php?id=34531&keywords=python&tid=87&lid=2218">TEG03-高級研發(fā)工程師(深圳)</a></td> <td>技術(shù)類</td> <td>1</td> <td>深圳</td> <td>2017-11-24</td> </tr> <tr class="odd"> <td class="l square"><a target="_blank" href="position_detail.php?id=34532&keywords=python&tid=87&lid=2218">TEG03-高級圖像算法研發(fā)工程師(深圳)</a></td> <td>技術(shù)類</td> <td>1</td> <td>深圳</td> <td>2017-11-24</td> </tr> <tr class="even"> <td class="l square"><a target="_blank" href="position_detail.php?id=31648&keywords=python&tid=87&lid=2218">TEG11-高級AI開發(fā)工程師(深圳)</a></td> <td>技術(shù)類</td> <td>4</td> <td>深圳</td> <td>2017-11-24</td> </tr> <tr class="odd"> <td class="l square"><a target="_blank" href="position_detail.php?id=32218&keywords=python&tid=87&lid=2218">15851-后臺開發(fā)工程師</a></td> <td>技術(shù)類</td> <td>1</td> <td>深圳</td> <td>2017-11-24</td> </tr> <tr class="even"> <td class="l square"><a target="_blank" href="position_detail.php?id=32217&keywords=python&tid=87&lid=2218">15851-后臺開發(fā)工程師</a></td> <td>技術(shù)類</td> <td>1</td> <td>深圳</td> <td>2017-11-24</td> </tr> <tr class="odd"> <td class="l square"><a id="test" class="test" target='_blank' href="position_detail.php?id=34511&keywords=python&tid=87&lid=2218">SNG11-高級業(yè)務(wù)運維工程師(深圳)</a></td> <td>技術(shù)類</td> <td>1</td> <td>深圳</td> <td>2017-11-24</td> </tr> </tbody> </table> """ soup = BeautifulSoup(html, 'lxml') print(soup, type(soup)) # 獲取所有的 tr 標簽 trs = soup.find_all("tr") for tr in trs: print(tr) print('*' * 30) # 獲取第二個 tr 標簽 tr = soup.find_all('tr')[1] print(tr) # 排除第一個tr值(通過切片的方式) jobMsg = soup.find_all('tr')[1:] print(jobMsg) # 獲取所有的 class = even 的 tr 標簽: # trs = soup.find_all('tr', class_='even') # class為關(guān)鍵字,不能直接用作變量名 trs = soup.find_all('tr', attrs={"class": "even"}) # 效果同上,如果有多個值,在后面添加即可,推薦 for tr in trs: print(tr) print('--' * 44) # 獲取所有a標簽里面的 href 屬性值: allA = soup.find_all('a') for a in allA: href = a.get('href') print(href) # 獲取所有的崗位信息 trs = soup.find_all('tr')[1:] # 把第一個的表頭去掉 # print(trs) for tr in trs: tds = tr.find_all('td') # 找到所有的 td jobName = tds[0].string # 獲取文本數(shù)據(jù),如果數(shù)據(jù)狠多,可以使用strings print(jobName)
以上就是Python數(shù)據(jù)解析之BeautifulSoup4的用法詳解的詳細內(nèi)容,更多關(guān)于Python BeautifulSoup4的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python應(yīng)用實現(xiàn)雙指數(shù)函數(shù)及擬合代碼實例
這篇文章主要介紹了Python應(yīng)用實現(xiàn)雙指數(shù)函數(shù)及擬合代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06Python列表list數(shù)組array用法實例解析
這篇文章主要介紹了Python列表list數(shù)組array用法,實例解析了關(guān)于數(shù)組的各種常見操作,非常具有實用價值,需要的朋友可以參考下2014-10-10淺談python內(nèi)置變量-reversed(seq)
下面小編就為大家?guī)硪黄獪\談python內(nèi)置變量-reversed(seq)。小編覺得挺不錯的。現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06pip matplotlib報錯equired packages can not be built解決
這篇文章主要介紹了pip matplotlib報錯equired packages can not be built解決,具有一定借鑒價值,需要的朋友可以參考下2018-01-01Django admin model 漢化顯示文字的實現(xiàn)方法
今天小編就為大家分享一篇Django admin model 漢化顯示文字的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08