如何利用Python爬蟲庫BeautifulSoup獲取對(duì)象(標(biāo)簽)名,屬性,內(nèi)容,注釋等操作下面就為大家介紹一下
一、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'])
1
2
['body', 'strikeout']
1
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>
1
2
如果轉(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中的字符串
```bash
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>
1
三、BeautifulSoup對(duì)象 BeautifulSoup 對(duì)象表示的是一個(gè)文檔的全部內(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?'
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
文章題目:如何使用BeautifulSoup爬蟲獲取對(duì)象名、屬性、注釋-創(chuàng)新互聯(lián)
標(biāo)題URL:http://chinadenli.net/article40/jjsho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、外貿(mào)建站、建站公司、定制網(wǎng)站、App開發(fā)、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容