溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python爬蟲庫(kù)BeautifulSoup如何獲取對(duì)象(標(biāo)簽)名,屬性,內(nèi)容,注釋

發(fā)布時(shí)間:2021-05-11 11:33:16 來源:億速云 閱讀:707 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了Python爬蟲庫(kù)BeautifulSoup如何獲取對(duì)象(標(biāo)簽)名,屬性,內(nèi)容,注釋,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

python的數(shù)據(jù)類型有哪些?

python的數(shù)據(jù)類型:1. 數(shù)字類型,包括int(整型)、long(長(zhǎng)整型)和float(浮點(diǎn)型)。2.字符串,分別是str類型和unicode類型。3.布爾型,Python布爾類型也是用于邏輯運(yùn)算,有兩個(gè)值:True(真)和False(假)。4.列表,列表是Python中使用最頻繁的數(shù)據(jù)類型,集合中可以放任何數(shù)據(jù)類型。5. 元組,元組用”()”標(biāo)識(shí),內(nèi)部元素用逗號(hào)隔開。6. 字典,字典是一種鍵值對(duì)的集合。7. 集合,集合是一個(gè)無序的、不重復(fù)的數(shù)據(jù)組合。

一、Tag(標(biāo)簽)對(duì)象

1.Tag對(duì)象與XML或HTML原生文檔中的tag相同。

from bs4 import BeautifulSoup
soup = BeautifulSoup('<b class="boldest">Extremely bold</b>','lxml')
tag = soup.b
type(tag)
bs4.element.Tag

2.Tag的Name屬性

每個(gè)tag都有自己的名字,通過.name來獲取

tag.name
'b'
tag.name = "blockquote" # 對(duì)原始文檔進(jìn)行修改
tag
<blockquote class="boldest">Extremely bold</blockquote>

3.Tag的Attributes屬性

獲取單個(gè)屬性

tag['class']
['boldest']

按字典的方式獲取全部屬性

tag.attrs
{'class': ['boldest']}

添加屬性

tag['class'] = 'verybold'
tag['id'] = 1
print(tag)
<blockquote class="verybold" id="1">Extremely bold</blockquote>

刪除屬性

del tag['class']
del tag['id']
tag
<blockquote>Extremely bold</blockquote>

4.Tag的多值屬性

多值屬性會(huì)返回一個(gè)列表

css_soup = BeautifulSoup('<p class="body strikeout"></p>','lxml')
print(css_soup.p['class'])
['body', 'strikeout']
rel_soup = BeautifulSoup('<p>Back to the <a rel="index">homepage</a></p>','lxml')
print(rel_soup.a['rel'])
rel_soup.a['rel'] = ['index', 'contents']
print(rel_soup.p)
['index']
<p>Back to the <a rel="index contents">homepage</a></p>

如果轉(zhuǎn)換的文檔是XML格式,那么tag中不包含多值屬性

xml_soup = BeautifulSoup('<p class="body strikeout"></p>', 'xml')
xml_soup.p['class']
'body strikeout'

二、可遍歷字符串(NavigableString)

1.字符串常被包含在tag內(nèi),使用NavigableString類來包裝tag中的字符串

from bs4 import BeautifulSoup
soup = BeautifulSoup('<b class="boldest">Extremely bold</b>','lxml')
tag = soup.b
print(tag.string)
print(type(tag.string))
Extremely bold
<class 'bs4.element.NavigableString'>

2.一個(gè) NavigableString 字符串與Python中的str字符串相同,通過str() 方法可以直接將 NavigableString 對(duì)象轉(zhuǎn)換成str字符串

unicode_string = str(tag.string)
print(unicode_string)
print(type(unicode_string))
Extremely bold
<class 'str'>

3.tag中包含的字符串不能編輯,但是可以被替換成其它的字符串,用 replace_with() 方法

tag.string.replace_with("No longer bold")
tag
<b class="boldest">No longer bold</b>

三、BeautifulSoup對(duì)象 BeautifulSoup 對(duì)象表示的是一個(gè)文檔的全部?jī)?nèi)容。

大部分時(shí)候,可以把它當(dāng)作 Tag 對(duì)象,它支持 遍歷文檔樹 和 搜索文檔樹 中描述的大部分的方法。

四、注釋與特殊字符串(Comment)對(duì)象

markup = "<b><!--Hey, buddy. Want to buy a used parser?--></b>"
soup = BeautifulSoup(markup,'lxml')
comment = soup.b.string
type(comment)
bs4.element.Comment

Comment 對(duì)象是一個(gè)特殊類型的 NavigableString 對(duì)象

comment
'Hey, buddy. Want to buy a used parser?'

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Python爬蟲庫(kù)BeautifulSoup如何獲取對(duì)象(標(biāo)簽)名,屬性,內(nèi)容,注釋”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI