Python中的BeautifulSoup模塊的用法-創(chuàng)新互聯(lián)

這篇文章主要介紹“Python中的Beautiful Soup模塊的用法”,在日常操作中,相信很多人在Python中的Beautiful Soup模塊的用法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Python中的Beautiful Soup模塊的用法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

創(chuàng)新互聯(lián)建站擁有網(wǎng)站維護(hù)技術(shù)和項(xiàng)目管理團(tuán)隊(duì),建立的售前、實(shí)施和售后服務(wù)體系,為客戶提供定制化的網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、網(wǎng)站維護(hù)、成都棕樹電信機(jī)房解決方案。為客戶網(wǎng)站安全和日常運(yùn)維提供整體管家式外包優(yōu)質(zhì)服務(wù)。我們的網(wǎng)站維護(hù)服務(wù)覆蓋集團(tuán)企業(yè)、上市公司、外企網(wǎng)站、成都做商城網(wǎng)站、政府網(wǎng)站等各類型客戶群體,為全球數(shù)千家企業(yè)提供全方位網(wǎng)站維護(hù)、服務(wù)器維護(hù)解決方案。

1.Beautiful Soup模塊的介紹

  • Beautiful Soup 是一個可以從HTML或XML文件中提取數(shù)據(jù)的Python庫,簡單來說,它能將HTML的標(biāo)簽文件解析成樹形結(jié)構(gòu),然后方便地獲取到指定標(biāo)簽的對應(yīng)屬性,還可以方便的實(shí)現(xiàn)全站點(diǎn)的內(nèi)容爬取和解析;

  • Beautiful Soup支持Python標(biāo)準(zhǔn)庫中的HTML解析器,還支持一些第三方的解析器,如果我們不安裝它,則 Python 會使用 Python默認(rèn)的解析器;
    lxml 是python的一個解析庫,支持HTML和XML的解析,html5lib解析器能夠以瀏覽器的方式解析,且生成HTML5文檔;

pip install beautifulsoup4
pip install html5lib
pip install lxml

2. Beautiful Soup模塊解析HTML文檔

假如現(xiàn)在有一段不完整的HTML代碼,我們現(xiàn)在要使用Beautiful Soup模塊來解析這段HTML代碼

data = '''                                         
<html><head><title>The Dormouse's story</title></he
<body>                                             
<p class="title"><b id="title">The Dormouse's story</b></p>   
<p class="story">Once upon a time there were three 
<a href="http://example.com/elsie" class="sister" i
<a href="http://example.com/lacie" class="sister" i
<a href="http://example.com/tillie" class="sister" 
and they lived at the bottom of a well.</p>        
<p class="story">...</p>                           
'''
  • 首先需要導(dǎo)入BeautifulSoup模塊,再實(shí)例化BeautifulSoup對象

from bs4 import BeautifulSoup           
soup = BeautifulSoup(data,'lxml')

然后通過BeautifulSoup提供的方法就可以拿到HTML的元素、屬性、鏈接、文本等,BeautifulSoup模塊可以將不完整的HTML文檔,格式化為完整的HTML文檔 ,比如我們打印print(soup.prettify())看一下輸出什么?

<html>
 <head>
  <title>
   The Dormouse's story
  </title>
 </head>
 <body>
  <p class="title">
   <b id="title">
    The Dormouse's story
   </b>
  </p>
  <p class="story">
   Once upon a time there were three
   <a a="" and="" at="" bottom="" class="sister" href="http://example.com/elsie" i="" lived="" of="" the="" they="" well.="">
    <p class="story">
     ...
    </p>
   </a>
  </p>
 </body>
</html>
  • 獲取標(biāo)簽,如title標(biāo)簽,a標(biāo)簽等

print('title = {}'.format(soup.title))             
# 輸出:title = <title>The Dormouse's story</title>
print('a={}'.format(soup.a))
  • 獲取標(biāo)簽的名稱,如title標(biāo)簽,body標(biāo)簽等

print('title_name = {}'.format(soup.title.name))
# 輸出:title_name = title
print('body_name = {}'.format(soup.body.name))
# 輸出:body_name = body
  • 獲取標(biāo)簽的內(nèi)容,如title標(biāo)簽

print('title_string = {}'.format(soup.title.string))
#  輸出:title_string = The Dormouse's story
  • 如果想要獲取某個標(biāo)簽的父標(biāo)簽的名稱,可以使用parent,如title標(biāo)簽,可以得到父標(biāo)簽head標(biāo)簽,且會自定補(bǔ)齊不完整的標(biāo)簽;

print('title_pareat_name = {}'.format(soup.title.parent))
# 輸出:title_pareat_name = <head><title>The Dormouse's story</title>
</head>
  • 獲取第一個p標(biāo)簽

print('p = {}'.format(soup.p))
# 輸出:p = <p class="title"><b>The Dormouse's story</b></p>
  • 獲取第一個p標(biāo)簽的class的值,獲取第一個a標(biāo)簽的class值

print('p_class = {}'.format(soup.p["class"]))
# 輸出:p_class = ['title']
print('a_class = {}'.format(soup.a["class"]))
# 輸出:a_class = ['sister']
  • 獲取所有的標(biāo)簽

#  獲取所有的a標(biāo)簽
print('a = {}'.format(soup.find_all('a')))
#  獲取所有的p標(biāo)簽  
print('p = {}'.format(soup.find_all('p')))
  • 獲取id為link3的標(biāo)簽

print('a_link = {}'.format(soup.find(id='title')))
# 輸出:a_link = <b id="title">The Dormouse's story</b>

3.BeautifulSoup中的對象

  • BeautifulSoup對象分為四類,分別是Tag(獲取標(biāo)簽), NavigableString(獲取標(biāo)簽內(nèi)容) , BeautifulSoup(根標(biāo)簽), Comment(標(biāo)簽內(nèi)的所有的文本)

語法:

  • soup.標(biāo)簽名:獲取HTML中的標(biāo)簽;

  • soup.標(biāo)簽名.name:獲取HTML中標(biāo)簽的名稱;

  • soup.標(biāo)簽名.attrs:獲取標(biāo)簽的所有屬性;

  • soup.標(biāo)簽名.string:獲取HTML中標(biāo)簽的文本內(nèi)容;

  • soup.標(biāo)簽名.parent:獲取HTML中標(biāo)簽的父標(biāo)簽;

  • prettify()方法:可以將Beautiful Soup的文檔樹格式化后以Unicode編碼輸出,每個XML/HTML標(biāo)簽都獨(dú)占一行;

4.遍歷文檔

  • contents:獲取所有子節(jié)點(diǎn),返回一個列表,可以通過下標(biāo)取值;

soup = BeautifulSoup(html,"lxml")
# 返回一個列表
print(soup.p.contents)
# 拿到第一個子節(jié)點(diǎn)
print(soup.p.contents[0])
  • children:返回子節(jié)點(diǎn)的生成器對象;

for tag in soup.p.children:
    print(tag)
  • soup.strings:獲取所有節(jié)點(diǎn)的內(nèi)容,包括空格;

soup = BeautifulSoup(html,"lxml")
for content in soup.strings:
    print(repr(content))
  • soup.stripped_strings:獲取所有節(jié)點(diǎn)的內(nèi)容,不包括空格;

soup = BeautifulSoup(html,"lxml")
for tag in soup.stripped_strings:
    print(repr(tag))

5.查找標(biāo)簽

  • find_all():查找所有指定標(biāo)簽名稱的子節(jié)點(diǎn)(可同時(shí)查找多個標(biāo)簽),并判斷是否符合過濾器的條件,返回一個列表;

soup = BeautifulSoup(html,"lxml")
print(soup.find_all('a'))
print(soup.find_all(['a','p']))
print(soup.find_all(re.compile('^a')))
  • find():和find_all()差不多,但是find_all() 方法的返回結(jié)果是值包含一個元素的列表,而 find() 方法直接返回結(jié)果;

soup = BeautifulSoup(html,"lxml")
print(soup.find('a'))

到此,關(guān)于“Python中的Beautiful Soup模塊的用法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

本文題目:Python中的BeautifulSoup模塊的用法-創(chuàng)新互聯(lián)
瀏覽地址:http://www.muchs.cn/article16/dpgidg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、用戶體驗(yàn)、品牌網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、網(wǎng)站營銷、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)