python bs4 的使用

  1. 導(dǎo)入
    from bs4 import BeautifulSoup
我們創(chuàng)建一個(gè)字符串,后面的例子我們便會用它來演示
  1. html = """
  2. <html><head><title>The Dormouse's story</title></head>
  3. <body>
  4. <p class="title" name="dromouse"><b>The Dormouse's story</b></p>
  5. <p class="story">Once upon a time there were three little sisters; and their names were
  6. <a class="sister" id="link1"></a>,
  7. <a class="sister" id="link2">Lacie</a> and
  8. <a class="sister" id="link3">Tillie</a>;
  9. and they lived at the bottom of a well.</p>
  10. <p class="story">...</p>
  11. """

創(chuàng)建 beautifulsoup 對象

  1. soup = BeautifulSoup(html)
另外,我們還可以用本地 HTML 文件來創(chuàng)建對象,例如
  1. soup = BeautifulSoup(open('index.html'))
上面這句代碼便是將本地 index.html 文件打開,用它來創(chuàng)建 soup 對象。下面我們來打印一下 soup 對象的內(nèi)容,格式化輸出
  1. print soup.prettify()
指定編碼:當(dāng)html為其他類型編碼(非utf-8和asc ii),比如GB2312的話,則需要指定相應(yīng)的字符編碼,BeautifulSoup才能正確解析。
  1. htmlCharset = "GB2312"

  2. soup = BeautifulSoup(respHtml, fromEncoding=htmlCharset)

  3. !/usr/bin/python

  4. -- coding: UTF-8 --

  5. from bs4 import BeautifulSoup

  6. import re

  7. 待分析字符串 :

html_doc = """
<html>
<head>
<title>The Dormouse's story</title>
</head>
<body>
<p class="title aq">
<b>
The Dormouse's story
</b>
</p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a class="sister" id="link1">Elsie</a>,
<a class="sister" id="link2">Lacie</a>
and
<a class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.
.</p>

<p class="story">...</p>
"""

html字符串創(chuàng)建BeautifulSoup對象 :
soup = BeautifulSoup(html_doc, 'html.parser', from_encoding='utf-8')

輸出第一個(gè) title 標(biāo)簽 :
print soup.title

輸出第一個(gè) title 標(biāo)簽的標(biāo)簽名稱 :
print soup.title.name

輸出第一個(gè) title 標(biāo)簽的包含內(nèi)容 :
print soup.title.string

輸出第一個(gè) title 標(biāo)簽的父標(biāo)簽的標(biāo)簽名稱 :
print soup.title.parent.name

輸出第一個(gè) p 標(biāo)簽
print soup.p

輸出第一個(gè) p 標(biāo)簽的 class 屬性內(nèi)容 :
print soup.p['class']

輸出第一個(gè) a 標(biāo)簽的 href 屬性內(nèi)容 :
print soup.a['href']
'''''
soup的屬性可以被添加,刪除或修改. 再說一次, soup的屬性操作方法與字典一樣
'''
修改第一個(gè) a 標(biāo)簽的href屬性為 http://www.baidu.com/
soup.a['href'] = 'http://www.baidu.com/'

給第一個(gè) a 標(biāo)簽添加 name 屬性 :
soup.a['name'] = u'百度'

刪除第一個(gè) a 標(biāo)簽的 class 屬性為 :
del soup.a['class']

輸出第一個(gè) p 標(biāo)簽的所有子節(jié)點(diǎn) :
print soup.p.contents

輸出第一個(gè) a 標(biāo)簽 :
print soup.a

輸出所有的 a 標(biāo)簽,以列表形式顯示 :
print soup.find_all('a')

輸出第一個(gè) id 屬性等于 link3 的 a 標(biāo)簽 :
print soup.find(id="link3")

獲取所有文字內(nèi)容 :
print(soup.get_text())

輸出第一個(gè) a 標(biāo)簽的所有屬性信息 :
print soup.a.attrs

for link in soup.find_all('a'):
獲取 link 的 href 屬性內(nèi)容
print(link.get('href'))

對soup.p的子節(jié)點(diǎn)進(jìn)行循環(huán)輸出 :
for child in soup.p.children:
print(child)

正則匹配,名字中帶有b的標(biāo)簽 :
for tag in soup.find_all(re.compile("b")):
print(tag.name)

import bs4#導(dǎo)入BeautifulSoup庫
Soup = BeautifulSoup(html)#其中html 可以是字符串,也可以是句柄
需要注意的是,BeautifulSoup會自動(dòng)檢測傳入文件的編碼格式,然后轉(zhuǎn)化為Unicode格式
通過如上兩句話,BS自動(dòng)把文檔生成為如上圖中的解析樹。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容