python爬蟲學(xué)習(xí)筆記--BeautifulSoup4庫(kù)的使用詳解
from bs4 import BeautifulSoup#創(chuàng)建 Beautiful Soup 對(duì)象# 使用lxml來進(jìn)行解析soup = BeautifulSoup(html,'lxml')print(soup.prettify())
返回結(jié)果

就是 HTML 中的一個(gè)個(gè)標(biāo)簽
在上面范例的基礎(chǔ)上添加
from bs4 import BeautifulSoup#創(chuàng)建 Beautiful Soup 對(duì)象# 使用lxml來進(jìn)行解析soup = BeautifulSoup(html,'lxml')#print(soup.prettify())#創(chuàng)建 Beautiful Soup 對(duì)象soup = BeautifulSoup(html,’lxml’)print (soup.title)#None因?yàn)檫@里沒有tiele標(biāo)簽所以返回noneprint (soup.head)#None因?yàn)檫@里沒有head標(biāo)簽所以返回noneprint (soup.a)#返回 <a target='_blank'>編輯自我介紹,讓更多人了解你<span class='write-icon'></span></a>print (type(soup.p))#返回 <class ’bs4.element.Tag’>print( soup.p)
其中print( soup.p)
返回結(jié)果為

同樣地,在上面地基礎(chǔ)上添加
print (soup.name)# [document] #soup 對(duì)象本身比較特殊,它的 name 即為 [document]
返回

print (soup.head.name)#head #對(duì)于其他內(nèi)部標(biāo)簽,輸出的值為標(biāo)簽本身的名稱
print (soup.p.attrs)##把p標(biāo)簽的所有屬性打印出來,得到的類型是一個(gè)字典。
返回

print (soup.p[’class’])#獲取P標(biāo)簽下地class標(biāo)簽
soup.p[’class’] = 'newClass'print (soup.p) # 可以對(duì)這些屬性和內(nèi)容等等進(jìn)行修改
返回

前面地基礎(chǔ)上添加
print (soup.p.string)# The Dormouse’s storyprint (type(soup.p.string))# <class ’bs4.element.NavigableString’>thon
返回結(jié)果

beautiful soup對(duì)象表示文檔的全部?jī)?nèi)容。大多數(shù)情況下,它可以被視為標(biāo)記對(duì)象。它支持遍歷文檔樹并搜索文檔樹中描述的大多數(shù)方法因?yàn)锽eauty soup對(duì)象不是真正的HTML或XML標(biāo)記,所以它沒有名稱和屬性。但是,有時(shí)查看其內(nèi)容很方便。Name屬性,因此美麗的湯對(duì)象包含一個(gè)特殊屬性。值為“[文檔]”的名稱
print(soup.name)#返回 ’[document]’常用的對(duì)象–Comment
用于解釋注釋部分的內(nèi)容
markup = '<b><!--Hey, buddy. Want to buy a used parser?--></b>'soup = BeautifulSoup(markup)comment = soup.b.stringtype(comment)# <class ’bs4.element.Comment’>對(duì)文檔樹的遍歷
在上面的基礎(chǔ)上添加
head_tag = soup.div# 返回所有子節(jié)點(diǎn)的列表print(head_tag.contents)
返回

同理
head_tag = soup.div# 返回所有子節(jié)點(diǎn)的迭代器for child in head_tag.children: print(child)
返回

可用 .strings 來循環(huán)獲取
for string in soup.strings: print(repr(string))
返回

for string in soup.stripped_strings: print(repr(string))
返回

找到所有
print(soup.find_all('a',id=’link2’))
find方法是找到第一個(gè)滿足條件的標(biāo)簽后立即返回,返回一個(gè)元素。find_all方法是把所有滿足條件的標(biāo)簽都選到,然后返回。
select方法(各種查找)#通過標(biāo)簽名查找:print(soup.select(’a’))#通過類名查找:#通過類名,則應(yīng)該在類的前面加一個(gè)’.’print(soup.select(’.sister’))#通過id查找:#通過id查找,應(yīng)該在id的名字前面加一個(gè)#號(hào)print(soup.select('#link1'))
查找a標(biāo)簽返回的結(jié)果

其他因?yàn)榫W(wǎng)頁本身沒有,返回的是一個(gè)空列表
組合查找
print(soup.select('p #link1'))#查找 p 標(biāo)簽中,id 等于 link1的內(nèi)容
子標(biāo)簽查找
print(soup.select('head > title'))
通過屬性查找
print(soup.select(’a[]’))#屬性與標(biāo)簽屬同一節(jié)點(diǎn),中間不能有空格獲取內(nèi)容
先查看類型
print (type(soup.select(’div’)))

for title in soup.select(’div’): print (title.get_text())
返回

print (soup.select(’div’)[20].get_text())#選取第20個(gè)div標(biāo)簽的內(nèi)容
返回

本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注好吧啦網(wǎng)的更多內(nèi)容!
相關(guān)文章:
1. python中文本字符處理的簡(jiǎn)單方法記錄2. 使用Blazor框架實(shí)現(xiàn)在前端瀏覽器中導(dǎo)入和導(dǎo)出Excel3. 如何從Python的cmd中獲得.py文件參數(shù)4. ASP基礎(chǔ)知識(shí)Command對(duì)象講解5. vscode運(yùn)行php報(bào)錯(cuò)php?not?found解決辦法6. Python-openpyxl表格讀取寫入的案例詳解7. Python使用Selenium自動(dòng)進(jìn)行百度搜索的實(shí)現(xiàn)8. JavaScript實(shí)現(xiàn)留言板實(shí)戰(zhàn)案例9. PHP laravel實(shí)現(xiàn)導(dǎo)出PDF功能10. JS中6個(gè)對(duì)象數(shù)組去重的方法

網(wǎng)公網(wǎng)安備