詳解BeautifulSoup獲取特定標(biāo)簽下內(nèi)容的方法
以下是個(gè)人在學(xué)習(xí)beautifulSoup過程中的一些總結(jié),目前我在使用爬蟲數(shù)據(jù)時(shí)使用的方法的是:先用find_all()找出需要內(nèi)容所在的標(biāo)簽,如果所需內(nèi)容一個(gè)find_all()不能滿足,那就用兩個(gè)或者多個(gè)。接下來遍歷find_all的結(jié)果,用get_txt()、get(‘href')、得到文本或者鏈接,然后放入各自的列表中。這樣做有一個(gè)缺點(diǎn)就是txt的數(shù)據(jù)是一個(gè)單獨(dú)的列表,鏈接的數(shù)據(jù)也是一個(gè)單獨(dú)的列表,一方面不能體現(xiàn)這些數(shù)據(jù)之間的結(jié)構(gòu)性,另一方面當(dāng)想要獲得更多的內(nèi)容時(shí),就要創(chuàng)建更多的空列表。
遍歷所有標(biāo)簽:
soup.find_all('a')
找出所有頁面中含有標(biāo)簽a的html語句,結(jié)果以列表形式存儲。對找到的標(biāo)簽可以進(jìn)一步處理,如用for對結(jié)果遍歷,可以對結(jié)果進(jìn)行purify,得到如鏈接,字符等結(jié)果。
# 創(chuàng)建空列表 links=[] txts=[] tags=soup.find_all('a') for tag in tags: links.append(tag.get('href') txts.append(tag.txt) #或者txts.append(tag.get_txt())
得到html的屬性名:
atr=[] tags=soup.find_all('a') for tag in tags: atr.append(tag.p('class')) # 得到a 標(biāo)簽下,子標(biāo)簽p的class名稱
find_all()的相關(guān)用法實(shí)例:
實(shí)例來自BeautifulSoup中文文檔
1. 字符串
最簡單的過濾器是字符串.在搜索方法中傳入一個(gè)字符串參數(shù),Beautiful Soup會查找與字符串完整匹配的內(nèi)容,下面的例子用于查找文檔中所有的標(biāo)簽:
soup.find_all('b') # [<b>The Dormouse's story</b>]
2.正則表達(dá)式
如果傳入正則表達(dá)式作為參數(shù),Beautiful Soup會通過正則表達(dá)式的 match() 來匹配內(nèi)容.下面例子中找出所有以b開頭的標(biāo)簽,這表示和標(biāo)簽都應(yīng)該被找到:
import re for tag in soup.find_all(re.compile("^b")): print(tag.name) # body # b
下面代碼找出所有名字中包含”t”的標(biāo)簽:
for tag in soup.find_all(re.compile("t")): print(tag.name) # html # title
3.列表
如果傳入列表參數(shù),Beautiful Soup會將與列表中任一元素匹配的內(nèi)容返回.下面代碼找到文檔中所有標(biāo)簽和標(biāo)簽:
soup.find_all(["a", "b"]) # [<b>The Dormouse's story</b>, # <a class="sister" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>, # <a class="sister" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>, # <a class="sister" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]
4.方法(自定義函數(shù),傳入find_all)
如果沒有合適過濾器,那么還可以定義一個(gè)方法,方法只接受一個(gè)元素參數(shù) [4] ,如果這個(gè)方法返回 True 表示當(dāng)前元素匹配并且被找到,如果不是則反回 False
下面方法校驗(yàn)了當(dāng)前元素,如果包含 class 屬性卻不包含 id 屬性,那么將返回 True:
def has_class_but_no_id(tag): return tag.has_attr('class') and not tag.has_attr('id')```
返回結(jié)果中只有
標(biāo)簽沒有標(biāo)簽,因?yàn)闃?biāo)簽還定義了”id”,沒有返回和,因?yàn)楹椭袥]有定義”class”屬性.
下面代碼找到所有被文字包含的節(jié)點(diǎn)內(nèi)容:
from bs4 import NavigableString def surrounded_by_strings(tag): return (isinstance(tag.next_element, NavigableString) and isinstance(tag.previous_element, NavigableString)) for tag in soup.find_all(surrounded_by_strings): print tag.name # p # a # a # a # p
5.按照CSS搜索
按照CSS類名搜索tag的功能非常實(shí)用,但標(biāo)識CSS類名的關(guān)鍵字 class 在Python中是保留字,使用 class 做參數(shù)會導(dǎo)致語法錯(cuò)誤.從Beautiful Soup的4.1.1版本開始,可以通過 class_ 參數(shù)搜索有指定CSS類名的tag:
soup.find_all("a", class_="sister") # [<a class="sister" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>, # <a class="sister" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>, # <a class="sister" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]
或者:
soup.find_all("a", attrs={"class": "sister"}) # [<a class="sister" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>, # <a class="sister" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>, # <a class="sister" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]
6.按照text參數(shù)查找
通過 text 參數(shù)可以搜搜文檔中的字符串內(nèi)容.與 name 參數(shù)的可選值一樣, text 參數(shù)接受 字符串 , 正則表達(dá)式 , 列表, True . 看例子:
soup.find_all(text="Elsie") # [u'Elsie'] soup.find_all(text=["Tillie", "Elsie", "Lacie"]) # [u'Elsie', u'Lacie', u'Tillie'] soup.find_all(text=re.compile("Dormouse")) [u"The Dormouse's story", u"The Dormouse's story"] def is_the_only_string_within_a_tag(s): ""Return True if this string is the only child of its parent tag."" return (s == s.parent.string) soup.find_all(text=is_the_only_string_within_a_tag) # [u"The Dormouse's story", u"The Dormouse's story", u'Elsie', u'Lacie', u'Tillie', u'...']
雖然 text 參數(shù)用于搜索字符串,還可以與其它參數(shù)混合使用來過濾tag.Beautiful Soup會找到 .string 方法與 text 參數(shù)值相符的tag.下面代碼用來搜索內(nèi)容里面包含“Elsie”的標(biāo)簽:
soup.find_all("a", text="Elsie") # [<a rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link1">Elsie</a>]
7.只查找當(dāng)前標(biāo)簽的子節(jié)點(diǎn)
調(diào)用tag的 find_all() 方法時(shí),Beautiful Soup會檢索當(dāng)前tag的所有子孫節(jié)點(diǎn),如果只想搜索tag的直接子節(jié)點(diǎn),可以使用參數(shù) recursive=False .
一段簡單的文檔:
<html> <head> <title> The Dormouse's story </title> </head> ...
是否使用 recursive 參數(shù)的搜索結(jié)果:
soup.html.find_all("title") # [<title>The Dormouse's story</title>] soup.html.find_all("title", recursive=False) # []
到此這篇關(guān)于詳解BeautifulSoup獲取特定標(biāo)簽下內(nèi)容的方法的文章就介紹到這了,更多相關(guān)BeautifulSoup獲取特定標(biāo)簽內(nèi)容內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于不懂Chromedriver如何配置環(huán)境變量問題解決方法
這篇文章主要介紹了關(guān)于不懂Chromedriver如何配置環(huán)境變量問題解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06pytorch加載自己的圖像數(shù)據(jù)集實(shí)例
這篇文章主要介紹了pytorch加載自己的圖像數(shù)據(jù)集實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)字典相關(guān)計(jì)算問題示例
這篇文章主要介紹了Python字典相關(guān)計(jì)算問題,結(jié)合實(shí)例形式總結(jié)分析了Python字典相關(guān)的最小值、最大值、排序等操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-02-02Python實(shí)現(xiàn)企業(yè)微信通知機(jī)器人的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)對企業(yè)微信進(jìn)行群通知的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-02-02Python基于sftp及rsa密匙實(shí)現(xiàn)遠(yuǎn)程拷貝文件的方法
這篇文章主要介紹了Python基于sftp及rsa密匙實(shí)現(xiàn)遠(yuǎn)程拷貝文件的方法,結(jié)合實(shí)例形式分析了基于RSA秘鑰遠(yuǎn)程登陸及文件操作的相關(guān)技巧,需要的朋友可以參考下2016-09-09django的聚合函數(shù)和aggregate、annotate方法使用詳解
這篇文章主要介紹了django的聚合函數(shù)和aggregate、annotate方法使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07快速掌握python權(quán)限功能設(shè)計(jì)實(shí)戰(zhàn)指南
在處理權(quán)限控制時(shí),裝飾器能幫助我們以一種統(tǒng)一且簡潔的方式管理不同用戶對系統(tǒng)資源的訪問權(quán)限,本文將通過幾個(gè)簡單的示例逐步展示如何利用Python裝飾器實(shí)現(xiàn)從基礎(chǔ)到復(fù)雜的權(quán)限控制功能2024-01-01python實(shí)現(xiàn)sm2和sm4國密(國家商用密碼)算法的示例
這篇文章主要介紹了python實(shí)現(xiàn)sm2和sm4國密(國家商用密碼)算法的示例,幫助大家使用python加密文件,感興趣的朋友可以了解下2020-09-09