Python爬蟲之CSS選擇器
CSS選擇器
這是另一種與find_all()方法有異曲同工的查詢方法,寫CSS時,標籤名不加任何修飾,類名前加.,id名前加#。
在這裡我們也可以利用類似的方法來篩選元素,用到的方法是soup.select(),返回的型別是list。
(1)透過標籤名查詢
#!/usr/bin/python3 # -*- coding:utf-8 -*- from bs4 import BeautifulSoup html = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title" name="dromouse"><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="" class="sister" id="link1"><!-- Elsie --></a>, <a href="" class="sister" id="link2">Lacie</a> and <a href="" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ # 建立 Beautiful Soup 物件,指定lxml解析器 soup = BeautifulSoup(html, "lxml") print(soup.select("title")) print(soup.select("b")) print(soup.select("a"))
執行結果
[<title>The Dormouse's story</title>] [<b>The Dormouse's story</b>] [<a class="sister" href="" id="link1"><!-- Elsie --></a>, <a class="sister" href="" id="link2">Lacie</a>, <a class="sister" href="" id="link3">Tillie</a>]
(2)透過類名查詢
#!/usr/bin/python3 # -*- coding:utf-8 -*- from bs4 import BeautifulSoup html = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title" name="dromouse"><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="" class="sister" id="link1"><!-- Elsie --></a>, <a href="" class="sister" id="link2">Lacie</a> and <a href="" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ # 建立 Beautiful Soup 物件,指定lxml解析器 soup = BeautifulSoup(html, "lxml") print(soup.select(".title"))
執行結果
[<p class="title" name="dromouse"><b>The Dormouse's story</b></p>]
(3)透過id名查詢
#!/usr/bin/python3 # -*- coding:utf-8 -*- from bs4 import BeautifulSoup html = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title" name="dromouse"><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="" class="sister" id="link1"><!-- Elsie --></a>, <a href="" class="sister" id="link2">Lacie</a> and <a href="" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ # 建立 Beautiful Soup 物件,指定lxml解析器 soup = BeautifulSoup(html, "lxml") print(soup.select("#link1"))
執行結果
[<p class="title" name="dromouse"><b>The Dormouse's story</b></p>]
相關推薦:《》
(4)組合查詢
#!/usr/bin/python3 # -*- coding:utf-8 -*- from bs4 import BeautifulSoup html = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title" name="dromouse"><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="" class="sister" id="link1"><!-- Elsie --></a>, <a href="" class="sister" id="link2">Lacie</a> and <a href="" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ # 建立 Beautiful Soup 物件,指定lxml解析器 soup = BeautifulSoup(html, "lxml") print(soup.select("p #link1"))
執行結果
[<a class="sister" href="" id="link1"><!-- Elsie --></a>]
(5)屬性查詢
查詢時還可以加入屬性元素,屬性需要用中括號括起來,注意屬性和標籤屬於同一節點,所以中間不能加空格,否則會無法匹配到。
#!/usr/bin/python3 # -*- coding:utf-8 -*- from bs4 import BeautifulSoup html = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title" name="dromouse"><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="" class="sister" id="link1"><!-- Elsie --></a>, <a href="" class="sister" id="link2">Lacie</a> and <a href="" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ # 建立 Beautiful Soup 物件,指定lxml解析器 soup = BeautifulSoup(html, "lxml") print(soup.select("a[class='sister']"))
執行結果
[<a class="sister" href="" id="link1"><!-- Elsie --></a>, <a class="sister" href="" id="link2">Lacie</a>, <a class="sister" href="" id="link3">Tillie</a>]
同樣,屬性仍然可以與上述查詢方式組合,不在同一節點的空格隔開,同一節點的不加空格。
#!/usr/bin/python3 # -*- coding:utf-8 -*- from bs4 import BeautifulSoup html = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title" name="dromouse"><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="" class="sister" id="link1"><!-- Elsie --></a>, <a href="" class="sister" id="link2">Lacie</a> and <a href="" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ # 建立 Beautiful Soup 物件,指定lxml解析器 soup = BeautifulSoup(html, "lxml") print(soup.select("p a[class='sister']"))
執行結果
[<a class="sister" href="" id="link1"><!-- Elsie --></a>, <a class="sister" href="" id="link2">Lacie</a>, <a class="sister" href="" id="link3">Tillie</a>]
(6)獲取內容
以上的select()方法返回的結果都是列表形式,可以遍歷形式輸出,然後用get_text()方法來獲取它的內容。
#!/usr/bin/python3 # -*- coding:utf-8 -*- from bs4 import BeautifulSoup html = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title" name="dromouse"><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="" class="sister" id="link1"><!-- Elsie --></a>, <a href="" class="sister" id="link2">Lacie</a> and <a href="" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ # 建立 Beautiful Soup 物件,指定lxml解析器 soup = BeautifulSoup(html, "lxml") print(soup.select("p a[class='sister']")) for item in soup.select("p a[class='sister']"): print(item.get_text())
執行結果
[<a class="sister" href="" id="link1"><!-- Elsie --></a>, <a class="sister" href="" id="link2">Lacie</a>, <a class="sister" href="" id="link3"> Tillie</a>] Lacie Tillie
注意:<!-- Elsie -->為註釋內容,未輸出
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1978/viewspace-2837232/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- pyspider 爬蟲教程(1):HTML 和 CSS 選擇器IDE爬蟲HTMLCSS
- 為什麼選擇Python做爬蟲Python爬蟲
- 如何選擇爬蟲工具?爬蟲
- Python網路爬蟲四大選擇器(正規表示式、BS4、Xpath、CSS)總結Python爬蟲CSS
- 爬蟲中資料清洗的選擇爬蟲
- CSS 選擇器CSS
- CSS選擇器CSS
- 【CSS】【3】CSS選擇器CSS
- CSS系列:CSS選擇器CSS
- CSS選擇器(5)——屬性選擇器CSS
- CSS ID選擇器與CLASS選擇器CSS
- 基本CSS選擇器,複合選擇器,後代選擇器CSS
- CSS 小結筆記之選擇器CSS筆記
- 爬蟲代理IP產品如何選擇爬蟲
- 為什麼很多人入門選擇Python爬蟲?Python爬蟲
- 為什麼爬蟲語言選擇Python而不是Java?爬蟲PythonJava
- CSS-選擇器6-兄弟選擇器CSS
- CSS :required 選擇器CSSUI
- CSS :optional 選擇器CSS
- css選擇器概述CSS
- CSS選擇器(一)CSS
- CSS 元素選擇器CSS
- CSS常用選擇器CSS
- CSS id選擇器CSS
- CSS_選擇器CSS
- CSS的選擇器CSS
- CSS--選擇器CSS
- Python爬蟲之BeautifulSoupPython爬蟲
- CSS之選擇器及其優先順序CSS
- 爬蟲之CSS語法學習爬蟲CSS
- 【Python學習】爬蟲爬蟲爬蟲爬蟲~Python爬蟲
- 為什麼爬蟲要選擇住宅代理?爬蟲
- css3 選擇器:屬性選擇器(五)CSSS3
- CSS-選擇器4-後代選擇器CSS
- CSS3選擇器02—CSS3部分選擇器CSSS3
- 【CSS】【4】CSS選擇器練習CSS
- CSS 選擇器權值CSS
- CSS :valid 選擇器CSS