前面兩篇介紹的是 Beautiful Soup 4 的基本對象類型和文檔樹的遍歷, 本篇介紹 Beautiful Soup 4 的文檔搜索
搜索文檔樹主要使用兩個方法 find() 和 find_all()
find_all():
find_all 是用於搜索節點中所有符合過濾條件的節點
那麼它支持哪些過濾器呢?
過濾器的類型:
字符串正則表達式列表True方法
字符串:
查找文檔中所有的標籤
soup.find_all('b')
正則表達式:
找出所有以b開頭的標籤
import refor tag in soup.find_all(re.compile("^b")): print(tag.name)
列表:
找到文檔中所有 a 標籤和 b 標籤
soup.find_all(["a", "b"])
True:
True 可以匹配任何值, 但是不會返回字符串節點
for tag in soup.find_all(True): print(tag.name)
方法:
可以定義一個方法, 方法只接受一個元素參數, 如果這個方法返回 True 表示當前元素匹配並且被找到, 如果不是則返回 False
這裡是官方文檔上面的例子:
下面代碼找到所有被文字包含的節點內容
find_all 的方法原型:
find_all( name , attrs , recursive , text , **kwargs )
name 參數:
name 參數可以查找所有名字為 name 的 tag, 字符串對象會被自動忽略掉
soup.find_all("p") 查找所有的 p 標籤
keyword 參數:
soup.find_all(id='link2',class_='title'), 這個將會查找到同時滿足這兩個屬性的標籤,這裡的class必須用class_傳入參數,因為class是python中的關鍵詞
有些屬性不能通過以上方法直接搜索,比如html5中的data-*屬性,不過可以通過attrs參數指定一個字典參數來搜索包含特殊屬性的標籤
data_soup.find_all(attrs={"data-foo": "value"})
text 參數:
通過 text 參數可以搜索文檔中的字符串內容, 與 name 參數的可選值一樣, text 參數接受 字符串 , 正則表達式 , 列表, True
soup.find_all("a", text="Elsie")
limit 參數:
find_all() 方法返回全部的搜索結構, 如果文檔樹很大那麼搜索會很慢, 如果我們不需要全部結果, 可以使用 limit 參數限制返回結果的數量, 效果與SQL中的limit關鍵字類似
soup.find_all("a", limit=2)
recursive 參數:
調用tag的 find_all() 方法時, Beautiful Soup 會檢索當前 tag 的所有子孫節點,如果只想搜索 tag 的直接子節點, 可以使用參數 recursive=False
soup.html.find_all("title", recursive=False)
find():
find( name , attrs , recursive , text , **kwargs )
find_all() 方法將返回文檔中符合條件的所有 tag, 儘管有時候我們只想得到一個結果, 比如文檔中只有一個標籤, 那麼使用 find_all() 方法來查找標籤就不太合適, 使用 find_all 方法並設置 limit=1 參數不如直接使用 find() 方法, 下面兩行代碼是等價的:
soup.find_all('title', limit=1)soup.find('title')
唯一的區別是 find_all() 方法的返回結果是值包含一個元素的列表, 而 find() 方法直接返回結果
find_all() 方法沒有找到目標是返回空列表, find() 方法找不到目標時, 返回 None.
CSS選擇器:
Beautiful Soup支持大部分的CSS選擇器: (更多請查閱官方文檔)
soup.select("body a") soup.select("html head title") soup.select("p > #link1") soup.select(".sister")
參考自 Beautiful Soup 4 官方文檔.
更多 python , php 原創文章, 關注我查看更多!