python爬蟲學(xué)習(xí)筆記--BeautifulSoup4庫的使用詳解
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ì)象表示文檔的全部內(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. jsp網(wǎng)頁實(shí)現(xiàn)貪吃蛇小游戲2. jsp+servlet簡單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))3. JavaScript實(shí)現(xiàn)組件化和模塊化方法詳解4. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息5. HTML5 Canvas繪制圖形從入門到精通6. .Net Core和RabbitMQ限制循環(huán)消費(fèi)的方法7. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式8. SpringMVC+Jquery實(shí)現(xiàn)Ajax功能9. ASP中if語句、select 、while循環(huán)的使用方法10. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明
