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

來源:互聯網
上載者:User

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/elsie” id=”link1”>Elsie</a>, # <a class=”sister” href=”http://example.com/lacie” id=”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>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.