摘要
Beautiful Soup 是一個可以從 HTML 或 XML 格式文件中提取數據的 Python 庫,他可以將HTML 或 XML 數據解析爲Python 對象,以方便通過Python代碼進行處理。
文檔環境
- 本文檔中代碼的測試環境
Beautifu Soup 使用說明
Beautiful Soup 的基本功能就是對HTML的標籤進行查找及編輯。
基本概念-對象類型
Beautiful Soup 將複雜 HTML 文檔轉換成一個複雜的樹形結構,每個節點都被轉換成一個Python 對象,Beautiful Soup將這些對象定義了4 種類型: Tag、NavigableString、BeautifulSoup、Comment 。
對象類型 | 描述 |
---|---|
BeautifulSoup | 文檔的全部內容 |
Tag | HTML的標籤 |
NavigableString | 標籤包含的文字 |
Comment | 是一種特殊的NavigableString類型,當標籤中的NavigableString 被註釋時,則定義爲該類型 |
安裝及引用
# Beautiful Soup
pip install bs4
# 解析器
pip install lxml
pip install html5lib
# 初始化
from bs4 import BeautifulSoup
# 方法一,直接打開文件
soup = BeautifulSoup(open("index.html"))
# 方法二,指定數據
resp = "<html>data</html>"
soup = BeautifulSoup(resp, 'lxml')
# soup 爲 BeautifulSoup 類型對象
print(type(soup))
標籤搜索及過濾
基本方法
標籤搜索有find_all() 和find() 兩個基本的搜索方法,find_all() 方法會返回所有匹配關鍵字的標籤列表,find()方法則只返回一個匹配結果。
soup = BeautifulSoup(resp, 'lxml')
# 返回一個標籤名爲"a"的Tag
soup.find("a")
# 返回所有tag 列表
soup.find_all("a")
## find_all方法可被簡寫
soup("a")
#找出所有以b開頭的標籤
for tag in soup.find_all(re.compile("^b")):
print(tag.name)
#找出列表中的所有標籤
soup.find_all(["a", "p"])
# 查找標籤名爲p,class屬性爲"title"
soup.find_all("p", "title")
# 查找屬性id爲"link2"
soup.find_all(id="link2")
# 查找存在屬性id的
soup.find_all(id=True)
#
soup.find_all(href=re.compile("elsie"), id='link1')
#
soup.find_all(attrs={"data-foo": "value"})
#查找標籤文字包含"sisters"
soup.find(string=re.compile("sisters"))
# 獲取指定數量的結果
soup.find_all("a", limit=2)
# 自定義匹配方法
def has_class_but_no_id(tag):
return tag.has_attr('class') and not tag.has_attr('id')
soup.find_all(has_class_but_no_id)
# 僅對屬性使用自定義匹配方法
def not_lacie(href):
return href and not re.compile("lacie").search(href)
soup.find_all(href=not_lacie)
# 調用tag的 find_all() 方法時,Beautiful Soup會檢索當前tag的所有子孫節點,如果只想搜索tag的直接子節點,可以使用參數 recursive=False
soup.find_all("title", recursive=False)
擴展方法
find_parents() | 所有父輩節點 |
find_parent() | 第一個父輩節點 |
find_next_siblings() | 之後的所有兄弟節點 |
find_next_sibling() | 之後的第一個兄弟節點 |
find_previous_siblings() | 之前的所有兄弟節點 |
find_previous_sibling() | 之前的第一個兄弟節點 |
find_all_next() | 之後的所有元素 |
find_next() | 之後的第一個元素 |
find_all_previous() | 之前的所有元素 |
find_previous() | 之前的第一個元素 |
CSS選擇器
Beautiful Soup支持大部分的CSS選擇器 http://www.w3.org/TR/CSS2/selector.html, 在 Tag 或 BeautifulSoup 對象的 .select() 方法中傳入字符串參數, 即可使用CSS選擇器的語法找到tag。
html_doc = """
<html>
<head>
<title>The Dormouse's story</title>
</head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">
Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a>
and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.
</p>
<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc)
# 所有 a 標籤
soup.select("a")
# 逐層查找
soup.select("body a")
soup.select("html head title")
# tag標籤下的直接子標籤
soup.select("head > title")
soup.select("p > #link1")
# 所有匹配標籤之後的兄弟標籤
soup.select("#link1 ~ .sister")
# 匹配標籤之後的第一個兄弟標籤
soup.select("#link1 + .sister")
# 根據calss類名
soup.select(".sister")
soup.select("[class~=sister]")
# 根據ID查找
soup.select("#link1")
soup.select("a#link1")
# 根據多個ID查找
soup.select("#link1,#link2")
# 根據屬性查找
soup.select('a[href]')
# 根據屬性值查找
soup.select('a[href^="http://example.com/"]')
soup.select('a[href$="tillie"]')
soup.select('a[href*=".com/el"]')
# 只獲取一個匹配結果
soup.select(".sister", limit=1)
# 只獲取一個匹配結果
soup.select_one(".sister")
標籤對象方法
標籤屬性
soup = BeautifulSoup('<p class="body strikeout" id="1">Extremely bold</p><p class="body strikeout" id="2">Extremely bold2</p>')
# 獲取所有的 p標籤對象
tags = soup.find_all("p")
# 獲取第一個p標籤對象
tag = soup.p
# 輸出標籤類型
type(tag)
# 標籤名
tag.name
# 標籤屬性
tag.attrs
# 標籤屬性class 的值
tag['class']
# 標籤包含的文字內容,對象NavigableString 的內容
tag.string
# 返回標籤內所有的文字內容
for string in tag.strings:
print(repr(string))
# 返回標籤內所有的文字內容, 並去掉空行
for string in tag.stripped_strings:
print(repr(string))
# 獲取到tag中包含的所有及包括子孫tag中的NavigableString內容,並以Unicode字符串格式輸出
tag.get_text()
## 以"|"分隔
tag.get_text("|")
## 以"|"分隔,不輸出空字符
tag.get_text("|", strip=True)
獲取子節點
tag.contents # 返回第一層子節點的列表
tag.children # 返回第一層子節點的listiterator 對象
for child in tag.children:
print(child)
tag.descendants # 遞歸返回所有子節點
for child in tag.descendants:
print(child)
獲取父節點
tag.parent # 返回第一層父節點標籤
tag.parents # 遞歸得到元素的所有父輩節點
for parent in tag.parents:
if parent is None:
print(parent)
else:
print(parent.name)
獲取兄弟節點
# 下一個兄弟元素
tag.next_sibling
# 當前標籤之後的所有兄弟元素
tag.next_siblings
for sibling in tag.next_siblings:
print(repr(sibling))
# 上一個兄弟元素
tag.previous_sibling
# 當前標籤之前的所有兄弟元素
tag.previous_siblings
for sibling in tag.previous_siblings:
print(repr(sibling))
元素的遍歷
Beautiful Soup中把每個tag定義爲一個“element”,每個“element”,被自上而下的在HTML中排列,可以通過遍歷命令逐個顯示標籤
# 當前標籤的下一個元素
tag.next_element
# 當前標籤之後的所有元素
for element in tag.next_elements:
print(repr(element))
# 當前標籤的前一個元素
tag.previous_element
# 當前標籤之前的所有元素
for element in tag.previous_elements:
print(repr(element))
修改標籤屬性
soup = BeautifulSoup('<b class="boldest">Extremely bold</b>')
tag = soup.b
tag.name = "blockquote"
tag['class'] = 'verybold'
tag['id'] = 1
tag.string = "New link text."
print(tag)
修改標籤內容(NavigableString)
soup = BeautifulSoup('<b class="boldest">Extremely bold</b>')
tag = soup.b
tag.string = "New link text."
添加標籤內容(NavigableString)
soup = BeautifulSoup("<a>Foo</a>")
tag = soup.a
tag.append("Bar")
tag.contents
# 或者
new_string = NavigableString("Bar")
tag.append(new_string)
print(tag)
添加註釋(Comment)
註釋是一個特殊的NavigableString 對象,所以同樣可以通過append() 方法進行添加。
from bs4 import Comment
soup = BeautifulSoup("<a>Foo</a>")
new_comment = soup.new_string("Nice to see you.", Comment)
tag.append(new_comment)
print(tag)
添加標籤(Tag)
添加標籤方法有兩種,一種是在指定標籤的內部添加(append方法),另一種是在指定位置添加(insert、insert_before、insert_after方法)
- append方法
soup = BeautifulSoup("<b></b>") tag = soup.b new_tag = soup.new_tag("a", href="http://www.example.com") new_tag.string = "Link text." tag.append(new_tag) print(tag)
* insert方法,是指在當前標籤子節點列表的指定位置插入對象(Tag或NavigableString)
```python
html = '<b><a href="http://example.com/">I linked to <i>example.com</i></a></b>'
soup = BeautifulSoup(html)
tag = soup.a
tag.contents
tag.insert(1, "but did not endorse ")
tag.contents
- insert_before() 和 insert_after() 方法則在當前標籤之前或之後的兄弟節點添加元素
html = '<b><a href="http://example.com/">I linked to <i>example.com</i></a></b>' soup = BeautifulSoup(html) tag = soup.new_tag("i") tag.string = "Don't" soup.b.insert_before(tag) soup.b
* wrap() 和 unwrap()可以對指定的tag元素進行包裝或解包,並返回包裝後的結果。
```python
# 添加包裝
soup = BeautifulSoup("<p>I wish I was bold.</p>")
soup.p.string.wrap(soup.new_tag("b"))
#輸出 <b>I wish I was bold.</b>
soup.p.wrap(soup.new_tag("div"))
#輸出 <div><p><b>I wish I was bold.</b></p></div>
# 拆解包裝
markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
soup = BeautifulSoup(markup)
a_tag = soup.a
a_tag.i.unwrap()
a_tag
#輸出 <a href="http://example.com/">I linked to example.com</a>
刪除標籤
html = '<b><a href="http://example.com/">I linked to <i>example.com</i></a></b>'
soup = BeautifulSoup(html)
# 清楚當前標籤的所有子節點
soup.b.clear()
# 將當前標籤及所有子節點從soup 中移除,返回當前標籤。
b_tag=soup.b.extract()
b_tag
soup
# 將當前標籤及所有子節點從soup 中移除,無返回。
soup.b.decompose()
# 將當前標籤替換爲指定的元素
tag=soup.i
new_tag = soup.new_tag("p")
new_tag.string = "Don't"
tag.replace_with(new_tag)
其他方法
輸出
# 格式化輸出
tag.prettify()
tag.prettify("latin-1")
- 使用Beautiful Soup解析後,文檔都被轉換成了Unicode,特殊字符也被轉換爲Unicode,如果將文檔轉換成字符串,Unicode編碼會被編碼成UTF-8.這樣就無法正確顯示HTML特殊字符了
- 使用Unicode時,Beautiful Soup還會智能的把“引號”轉換成HTML或XML中的特殊字符
文檔編碼
使用Beautiful Soup解析後,文檔都被轉換成了Unicode,其使用了“編碼自動檢測”子庫來識別當前文檔編碼並轉換成Unicode編碼。
soup = BeautifulSoup(html)
soup.original_encoding
# 也可以手動指定文檔的編碼
soup = BeautifulSoup(html, from_encoding="iso-8859-8")
soup.original_encoding
# 爲提高“編碼自動檢測”的檢測效率,也可以預先排除一些編碼
soup = BeautifulSoup(markup, exclude_encodings=["ISO-8859-7"])
- 通過Beautiful Soup輸出文檔時,不管輸入文檔是什麼編碼方式,默認輸出編碼均爲UTF-8編碼
文檔解析器
Beautiful Soup目前支持, “lxml”, “html5lib”, 和 “html.parser”
soup=BeautifulSoup("<a><b /></a>")
soup
#輸出: <html><body><a><b></b></a></body></html>
soup=BeautifulSoup("<a></p>", "lxml")
soup
#輸出: <html><body><a></a></body></html>
soup=BeautifulSoup("<a></p>", "html5lib")
soup
#輸出: <html><head></head><body><a><p></p></a></body></html>
soup=BeautifulSoup("<a></p>", "html.parser")
soup
#輸出: <a></a>