1. 程式人生 > >Python爬蟲包 BeautifulSoup 學習(十一) CSS 選擇器

Python爬蟲包 BeautifulSoup 學習(十一) CSS 選擇器

BeautifulSoup支援最常用的CSS選擇器,在 Tag 或 BeautifulSoup 物件的 .select() 方法中傳入字串引數,即可使用CSS選擇器的語法找到tag。

CSS選擇器

CSS選擇器是一種單獨的文件搜尋語法。
詳情請見此連結

BS4中的CSS選擇器

本篇所使用的html為:

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> """

你可以這樣搜尋tag:

soup.select("title")
# [<title>The Dormouse's story</title>]

soup.select("p nth-of-type(3)")
# [<p class="story">...</p>]

另外,你也可以搜尋在其他父標籤內部的標籤,即通過標籤的所屬關係尋找標籤

soup.select("body a")   #搜尋在body標籤內部的a標籤
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
#  <a class="sister" href="http://example.com/lacie"  id="link2">Lacie</a>,
#  <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

soup.select("html head title")  #搜尋在html->head標籤內部的標籤
# [<title>The Dormouse's story</title>]

可以直接尋找在其他標籤內部的標籤

soup.select("head > title")
# [<title>The Dormouse's story</title>]

soup.select("p > a")
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
#  <a class="sister" href="http://example.com/lacie"  id="link2">Lacie</a>,
#  <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

soup.select("p > a:nth-of-type(2)")
# [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

soup.select("p > #link1")
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]

soup.select("body > a")
# []

通過tags標籤獲得元素的同胞兄弟:

soup.select("#link1 ~ .sister")  #獲得id為link1,class為sister的兄弟標籤內容(所有的兄弟便籤)
# [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
#  <a class="sister" href="http://example.com/tillie"  id="link3">Tillie</a>]

soup.select("#link1 + .sister")   #獲得id為link1,class為sister的兄弟標籤內容(下一個兄弟便籤)
# [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

通過CSS的類獲得tags標籤:

soup.select(".sister") #獲得所有class為sister的標籤
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
#  <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
#  <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

soup.select("[class~=sister]")  #效果同上一個
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
#  <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
#  <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

通過id獲得標籤:

soup.select("#link1") #通過設定引數為id來獲取該id對應的tag
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]

soup.select("a#link2")  #這裡區別於上一個單純的使用id,又增添了tag屬性,使查詢更加具體
# [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

通過設定select函式的引數為列表,來獲取tags。只要匹配列表中的任意一個則就可以捕獲。

soup.select(“#link1,#link2”) #捕獲id為link1或link2的標籤
# [<a class=”sister” href=”http://example.com/elsieid=”link1”>Elsie</a>, 
# <a class=”sister” href=”http://example.com/lacieid=”link2”>Lacie</a>]

按照標籤是否存在某個屬性來獲取:

soup.select('a[href]') #獲取a標籤中具有href屬性的標籤
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
#  <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
#  <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

通過某個標籤的具體某個屬性值來查詢tags:

soup.select('a[href="http://example.com/elsie"]')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]

soup.select('a[href^="http://example.com/"]')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
#  <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
#  <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

soup.select('a[href$="tillie"]')
# [<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

soup.select('a[href*=".com/el"]')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]

這裡需要解釋一下:
soup.select(‘a[href^=”http://example.com/”]’)意思是查詢href屬性值是以”http://example.com/“值為開頭的標籤,可以檢視部落格介紹。
soup.select(‘a[href$=”tillie”]’)意思是查詢href屬性值是以tillie為結尾的標籤。
soup.select(‘a[href*=”.com/el”]’)意思是查詢href屬性值中存在字串”.com/el”的標籤,所以只有href=”http://example.com/elsie”一個匹配。

查詢符合查詢條件的第一個標籤:

soup.select_one(".sister") #只查詢符合條件的第一個tag
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>