Python爬蟲庫(kù)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都有自己的名字,通過(guò).name來(lái)獲取
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類來(lái)包裝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'>
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?'
更多關(guān)于Python爬蟲庫(kù)BeautifulSoup的使用方法請(qǐng)查看下面的相關(guān)鏈接
- python BeautifulSoup使用方法詳解
- Python BeautifulSoup [解決方法] TypeError: list indices must be integers or slices, not str
- Python BeautifulSoup中文亂碼問(wèn)題的2種解決方法
- python 解析html之BeautifulSoup
- python中bs4.BeautifulSoup的基本用法
- python基于BeautifulSoup實(shí)現(xiàn)抓取網(wǎng)頁(yè)指定內(nèi)容的方法
- Python使用BeautifulSoup庫(kù)解析HTML基本使用教程
- python爬蟲之BeautifulSoup 使用select方法詳解
- python爬蟲入門教程--HTML文本的解析庫(kù)BeautifulSoup(四)
- 從零開始學(xué)習(xí)Python與BeautifulSoup網(wǎng)頁(yè)數(shù)據(jù)抓取
相關(guān)文章
python實(shí)現(xiàn)將元祖轉(zhuǎn)換成數(shù)組的方法
這篇文章主要介紹了python實(shí)現(xiàn)將元祖轉(zhuǎn)換成數(shù)組的方法,涉及Python中l(wèi)ist方法的使用技巧,需要的朋友可以參考下2015-05-05Python日期格式和字符串格式相互轉(zhuǎn)換的方法
這篇文章主要介紹了Python日期格式和字符串格式相互轉(zhuǎn)換的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02如何使用draw.io插件在vscode中一體化導(dǎo)出高質(zhì)量圖片
這篇文章主要介紹了draw.io插件在vscode中一體化導(dǎo)出高質(zhì)量圖片需要的工具是vscode,?draw.io擴(kuò)展,draw.io桌面版?、python,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒,需要的朋友可以參考下2022-08-08keras讀取訓(xùn)練好的模型參數(shù)并把參數(shù)賦值給其它模型詳解
這篇文章主要介紹了keras讀取訓(xùn)練好的模型參數(shù)并把參數(shù)賦值給其它模型詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06Python面向?qū)ο缶幊讨庋b的藝術(shù)你了解嗎
這篇文章主要為大家詳細(xì)介紹了Python面向?qū)ο缶幊讨庋b的藝術(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02Python和Anaconda和Pycharm安裝教程圖文詳解
PyCharm是一種PythonIDE,帶有一整套可以幫助用戶在使用Python語(yǔ)言開發(fā)時(shí)提高其效率的工具,這篇文章主要介紹了Python和Anaconda和Pycharm安裝教程,需要的朋友可以參考下2020-02-02