欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python利用Beautiful Soup模塊搜索內(nèi)容詳解

 更新時間:2017年03月29日 08:43:57   作者:Glumes  
這篇文章主要給大家介紹了python中 Beautiful Soup 模塊的搜索方法函數(shù)。 方法不同類型的過濾參數(shù)能夠進(jìn)行不同的過濾,得到想要的結(jié)果。文中介紹的非常詳細(xì),對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。

前言

我們將利用 Beautiful Soup 模塊的搜索功能,根據(jù)標(biāo)簽名稱、標(biāo)簽屬性、文檔文本和正則表達(dá)式來搜索。

搜索方法

Beautiful Soup 內(nèi)建的搜索方法如下:

  • find()
  • find_all()
  • find_parent()
  • find_parents()
  • find_next_sibling()
  • find_next_siblings()
  • find_previous_sibling()
  • find_previous_siblings()
  • find_previous()
  • find_all_previous()
  • find_next()
  • find_all_next()

使用 find() 方法搜索

首先還是需要建立一個 HTML 文件用來做測試。

<html>
<body>
<div class="ecopyramid">
 <ul id="producers">
 <li class="producerlist">
  <div class="name">plants</div>
  <div class="number">100000</div>
 </li>
 <li class="producerlist">
  <div class="name">algae</div>
  <div class="number">100000</div>
 </li>
 </ul>
 <ul id="primaryconsumers">
 <li class="primaryconsumerlist">
  <div class="name">deer</div>
  <div class="number">1000</div>
 </li>
 <li class="primaryconsumerlist">
  <div class="name">rabbit</div>
  <div class="number">2000</div>
 </li>
 </ul>
 <ul id="secondaryconsumers">
 <li class="secondaryconsumerlist">
  <div class="name">fox</div>
  <div class="number">100</div>
 </li>
 <li class="secondaryconsumerlist">
  <div class="name">bear</div>
  <div class="number">100</div>
 </li>
 </ul>
 <ul id="tertiaryconsumers">
 <li class="tertiaryconsumerlist">
  <div class="name">lion</div>
  <div class="number">80</div>
 </li>
 <li class="tertiaryconsumerlist">
  <div class="name">tiger</div>
  <div class="number">50</div>
 </li>
 </ul>
</div>
</body>
</html>

我們可以通過 find() 方法來獲得 <ul> 標(biāo)簽,默認(rèn)情況下會得到第一個出現(xiàn)的。接著再獲取 <li> 標(biāo)簽,默認(rèn)情況下還是會得到第一個出現(xiàn)的,接著獲得 <div> 標(biāo)簽,通過輸出內(nèi)容來驗證是否獲取了第一個出現(xiàn)的標(biāo)簽。

from bs4 import BeautifulSoup
with open('search.html','r') as filename:
 soup = BeautifulSoup(filename,'lxml')
first_ul_entries = soup.find('ul')
print first_ul_entries.li.div.string

find() 方法具體如下:

find(name,attrs,recursive,text,**kwargs) 

正如上代碼所示,find() 方法接受五個參數(shù):name、attrs、recursive、text 和 **kwargs 。name 、attrs 和 text 參數(shù)都可以在 find() 方法充當(dāng)過濾器,提高匹配結(jié)果的精確度。

搜索標(biāo)簽

除了上面代碼的搜索 <ul> 標(biāo)簽外,我們還可以搜索 <li> 標(biāo)簽,返回結(jié)果也是返回出現(xiàn)的第一個匹配內(nèi)容。

tag_li = soup.find('li')
# tag_li = soup.find(name = "li")
print type(tag_li)
print tag_li.div.string

搜索文本

如果我們只想根據(jù)文本內(nèi)容來搜索的話,我們可以只傳入文本參數(shù) :

search_for_text = soup.find(text='plants')
print type(search_for_text)
<class 'bs4.element.NavigableString'>

返回的結(jié)果也是 NavigableString 對象 。

根據(jù)正則表達(dá)式搜索

如下的一段 HTML 文本內(nèi)容

<div>The below HTML has the information that has email ids.</div>
 abc@example.com 
<div>xyz@example.com</div> 
 <span>foo@example.com</span>

可以看到 abc@example 郵件地址并沒有包括在任何標(biāo)簽內(nèi),這樣就不能根據(jù)標(biāo)簽來找到郵件地址了。這個時候,我們可以使用正則表達(dá)式來進(jìn)行匹配。

email_id_example = """
 <div>The below HTML has the information that has email ids.</div>
 abc@example.com
 <div>xyz@example.com</div>
 <span>foo@example.com</span>
 """
email_soup = BeautifulSoup(email_id_example,'lxml')
print email_soup
# pattern = "\w+@\w+\.\w+"
emailid_regexp = re.compile("\w+@\w+\.\w+")
first_email_id = email_soup.find(text=emailid_regexp)
print first_email_id

在使用正則表達(dá)式進(jìn)行匹配時,如果有多個匹配項,也是先返回第一個。

根據(jù)標(biāo)簽屬性值搜索

可以通過標(biāo)簽的屬性值來搜索:

search_for_attribute = soup.find(id='primaryconsumers')
print search_for_attribute.li.div.string

根據(jù)標(biāo)簽屬性值來搜索對大多數(shù)屬性都是可用的,例如:id、style 和 title 。

但是對以下兩種情況會有不同:

  • 自定義屬性
  • 類 ( class ) 屬性

我們不能再直接使用屬性值來搜索了,而是得使用 attrs 參數(shù)來傳遞給 find() 函數(shù)。

根據(jù)自定義屬性來搜索

在 HTML5 中是可以給標(biāo)簽添加自定義屬性的,例如給標(biāo)簽添加 屬性。

如下代碼所示,如果我們再像搜索 id 那樣進(jìn)行操作的話,會報錯的,Python 的變量不能包括 - 符號。

customattr = """
 <p data-custom="custom">custom attribute example</p>
   """
customsoup = BeautifulSoup(customattr,'lxml')
customsoup.find(data-custom="custom")
# SyntaxError: keyword can't be an expression

這個時候使用 attrs 屬性值來傳遞一個字典類型作為參數(shù)進(jìn)行搜索:

using_attrs = customsoup.find(attrs={'data-custom':'custom'})
print using_attrs

基于 CSS 中的 類 進(jìn)行搜索

對于 CSS 的類屬性,由于在 Python 中 class 是個關(guān)鍵字,所以是不能當(dāng)做標(biāo)簽屬性參數(shù)傳遞的,這種情況下,就和自定義屬性一樣進(jìn)行搜索。也是使用 attrs 屬性,傳遞一個字典進(jìn)行匹配 。

除了使用 attrs 屬性之外,還可以使用 class_ 屬性進(jìn)行傳遞,這樣與 class 區(qū)別開了,也不會導(dǎo)致錯誤。

css_class = soup.find(attrs={'class':'producerlist'})
css_class2 = soup.find(class_ = "producerlist")
print css_class
print css_class2

使用自定義的函數(shù)搜索

可以給 find() 方法傳遞一個函數(shù),這樣就會根據(jù)函數(shù)定義的條件進(jìn)行搜索。

函數(shù)應(yīng)該返回 true 或者是 false 值。

def is_producers(tag):
 return tag.has_attr('id') and tag.get('id') == 'producers'
tag_producers = soup.find(is_producers)
print tag_producers.li.div.string

代碼中定義了一個 is_producers 函數(shù),它將檢查標(biāo)簽是否具體 id 屬性以及屬性值是否等于 producers,如果符合條件則返回 true ,否則返回 false 。

聯(lián)合使用各種搜索方法

Beautiful Soup 提供了各種搜索方法,同樣,我們也可以聯(lián)合使用這些方法來進(jìn)行匹配,提高搜索的準(zhǔn)確度。

combine_html = """
 <p class="identical">
  Example of p tag with class identical
 </p>
 <div class="identical">
  Example of div tag with class identical
 <div>
 """
combine_soup = BeautifulSoup(combine_html,'lxml')
identical_div = combine_soup.find("div",class_="identical")
print identical_div

使用 find_all() 方法搜索

使用 find() 方法會從搜索結(jié)果中返回第一個匹配的內(nèi)容,而 find_all() 方法則會返回所有匹配的項。

find() 方法中用到的過濾項,同樣可以用在 find_all() 方法中。事實上,它們可以用到任何搜索方法中,例如:find_parents()find_siblings() 中 。

# 搜索所有 class 屬性等于 tertiaryconsumerlist 的標(biāo)簽。
all_tertiaryconsumers = soup.find_all(class_='tertiaryconsumerlist')
print type(all_tertiaryconsumers)
for tertiaryconsumers in all_tertiaryconsumers:
 print tertiaryconsumers.div.string

find_all() 方法為 :

find_all(name,attrs,recursive,text,limit,**kwargs)

它的參數(shù)和 find() 方法有些類似,多個了 limit 參數(shù)。limit 參數(shù)是用來限制結(jié)果數(shù)量的。而 find() 方法的 limit 就是 1 了。

同時,我們也能傳遞一個字符串列表的參數(shù)來搜索標(biāo)簽、標(biāo)簽屬性值、自定義屬性值和 CSS 類。

# 搜索所有的 div 和 li 標(biāo)簽
div_li_tags = soup.find_all(["div","li"])
print div_li_tags
print
# 搜索所有類屬性是 producerlist 和 primaryconsumerlist 的標(biāo)簽
all_css_class = soup.find_all(class_=["producerlist","primaryconsumerlist"])
print all_css_class
print

搜索相關(guān)標(biāo)簽

一般情況下,我們可以使用 find()find_all() 方法來搜索指定的標(biāo)簽,同時也能搜索其他與這些標(biāo)簽相關(guān)的感興趣的標(biāo)簽。

搜索父標(biāo)簽

可以使用 find_parent() 或者 find_parents() 方法來搜索標(biāo)簽的父標(biāo)簽。

find_parent() 方法將返回第一個匹配的內(nèi)容,而 find_parents() 將返回所有匹配的內(nèi)容,這一點與 find() find_all() 方法類似。

# 搜索 父標(biāo)簽
primaryconsumers = soup.find_all(class_='primaryconsumerlist')
print len(primaryconsumers)
# 取父標(biāo)簽的第一個
primaryconsumer = primaryconsumers[0]
# 搜索所有 ul 的父標(biāo)簽
parent_ul = primaryconsumer.find_parents('ul')
print len(parent_ul)
# 結(jié)果將包含父標(biāo)簽的所有內(nèi)容
print parent_ul
print
# 搜索,取第一個出現(xiàn)的父標(biāo)簽.有兩種操作
immediateprimary_consumer_parent = primaryconsumer.find_parent()
# immediateprimary_consumer_parent = primaryconsumer.find_parent('ul')
print immediateprimary_consumer_parent

搜索同級標(biāo)簽

Beautiful Soup 還提供了搜索同級標(biāo)簽的功能。

使用函數(shù) find_next_siblings() 函數(shù)能夠搜索同一級的下一個所有標(biāo)簽,而 find_next_sibling() 函數(shù)能夠搜索同一級的下一個標(biāo)簽。

producers = soup.find(id='producers')
next_siblings = producers.find_next_siblings()
print next_siblings

同樣,也可以使用 find_previous_siblings() find_previous_sibling() 方法來搜索上一個同級的標(biāo)簽。

搜索下一個標(biāo)簽

使用 find_next() 方法將搜索下一個標(biāo)簽中第一個出現(xiàn)的,而 find_next_all() 將會返回所有下級的標(biāo)簽項。

# 搜索下一級標(biāo)簽
first_div = soup.div
all_li_tags = first_div.find_all_next("li")
print all_li_tags

搜索上一個標(biāo)簽

與搜索下一個標(biāo)簽類似,使用 find_previous()find_all_previous() 方法來搜索上一個標(biāo)簽。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家學(xué)習(xí)或者使用python能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • 快速進(jìn)修Python指南之面向?qū)ο筮M(jìn)階

    快速進(jìn)修Python指南之面向?qū)ο筮M(jìn)階

    這篇文章主要為大家介紹了Java開發(fā)者快速進(jìn)修Python指南之面向?qū)ο筮M(jìn)階,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Python音頻處理庫pydub的使用教程詳解

    Python音頻處理庫pydub的使用教程詳解

    Pydub是Python音頻處理庫,可以對音頻進(jìn)行切割、合并、轉(zhuǎn)換、調(diào)整音量等操作。本文將對pydub各個知識點和案例進(jìn)行介紹,需要的可以參考一下
    2023-03-03
  • python使用Plotly繪圖工具繪制氣泡圖

    python使用Plotly繪圖工具繪制氣泡圖

    這篇文章主要為大家詳細(xì)介紹了python使用Plotly繪圖工具繪制氣泡圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Python類綁定方法及非綁定方法實例解析

    Python類綁定方法及非綁定方法實例解析

    這篇文章主要介紹了Python類綁定方法及非綁定方法實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • python流水線框架pypeln的安裝使用教程

    python流水線框架pypeln的安裝使用教程

    這篇文章主要介紹了python流水線框架pypeln的安裝使用教程,通過安裝pip install pypeln,基本元素在文中給大家介紹過,需要的朋友可以參考下
    2021-05-05
  • Python堆棧的具體使用

    Python堆棧的具體使用

    本文主要介紹了Python堆棧的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11
  • python中with的具體用法

    python中with的具體用法

    本文主要介紹了python中with的基本使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • python使用tkinter實現(xiàn)自定義多參數(shù)對話框

    python使用tkinter實現(xiàn)自定義多參數(shù)對話框

    Tkinter模塊是Python標(biāo)準(zhǔn)庫中的一部分,用于創(chuàng)建圖形用戶界面(GUI)應(yīng)用程序,它提供了一組工具和組件,用于創(chuàng)建窗口、按鈕、文本框等用戶界面元素,并且可以響應(yīng)用戶的輸入,本文將給大家講講python如何使用tkinter實現(xiàn)自定義多參數(shù)對話框
    2023-08-08
  • python文件操作相關(guān)知識點總結(jié)整理

    python文件操作相關(guān)知識點總結(jié)整理

    這篇文章主要介紹了python文件操作相關(guān)知識點,整理匯總了Python文件操作所涉及的常見函數(shù)與方法,并給出了實例代碼予以總結(jié)歸納,需要的朋友可以參考下
    2016-02-02
  • 淺談一下關(guān)于Python對XML的解析

    淺談一下關(guān)于Python對XML的解析

    這篇文章主要介紹了淺談一下關(guān)于Python對XML的解析,XML是一套定義語義標(biāo)記的規(guī)則,這些標(biāo)記將文檔分成許多部件并對這些部件加以標(biāo)識,需要的朋友可以參考下
    2023-05-05

最新評論