Python爬虫包 BeautifulSoup 学习(十一) CSS 选择器
来源:互联网 发布:美国航空怎么样知乎 编辑:程序博客网 时间:2024/06/05 10:58
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>
0 0
- Python爬虫包 BeautifulSoup 学习(十一) CSS 选择器
- Python爬虫学习纪要(十一):BeautifulSoup相关知识点3
- Python爬虫包 BeautifulSoup 学习(一) 简介与安装
- Python爬虫包 BeautifulSoup 学习(二) 异常处理
- Python爬虫包 BeautifulSoup 学习(三) 实例
- Python爬虫包 BeautifulSoup 学习(五) 实例
- Python爬虫包 BeautifulSoup 学习(六) 递归抓取
- Python爬虫包 BeautifulSoup 学习(七) children等应用
- Python爬虫包 BeautifulSoup 学习(八) parent等应用
- python beautifulsoup 爬虫学习
- BeautifulSoup学习之CSS选择器
- python学习之 beautifulsoup选择器
- Python爬虫包 BeautifulSoup 学习(四) bs基本对象与函数
- Python爬虫包 BeautifulSoup 学习(九) 正则表达式与Lambda表达式
- Python爬虫包 BeautifulSoup 学习(十) 各种html解析器的比较及使用
- 【网络爬虫入门04】彻底掌握BeautifulSoup的CSS选择器
- Python爬虫学习纪要(一):BeautifulSoup相关知识点
- Python爬虫学习纪要(二):BeautifulSoup相关知识点2
- synx的使用及出现的问题
- 海康 rtsp 主流 和 辅流的一个特点
- Array238ProductOfArrayExceptSelf
- spring cloud-使用feign来消费Restful服务同时加入Ribbon来实现负载均衡
- 2016年工作总结
- Python爬虫包 BeautifulSoup 学习(十一) CSS 选择器
- POJ3253 Fence Repair (HDU 1263 搬水果)
- PHP Study
- IS_REACHABLE
- android-webView拉起其他应用程序
- Eclipse 使用Maven
- mybatis详解(二)
- bcd与hex转换、以及编码格式
- [nRF51822] 16、nRF51822的随机数生成器,及随机数生成器的一些知识(可以帮您补补随机数发生器的知识)_0