python爬虫:BeautifulSoup 使用select方法的使用
来源:互联网 发布:ubuntu无法登录界面 编辑:程序博客网 时间:2024/06/05 08:33
1 html = """ 2 <html><head><title>The Dormouse's story</title></head> 3 <body> 4 <p class="title" name="dromouse"><b>The Dormouse's story</b></p> 5 <p class="story">Once upon a time there were three little sisters; and their names were 6 <a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>, 7 <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and 8 <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; 9 and they lived at the bottom of a well.</p>10 <p class="story">...</p>11 """
我们在写 CSS 时,标签名不加任何修饰,类名(class="className"引号内即为类名)前加点,id名(id="idName"引号前即为id名)前加 #,在这里我们也可以利用类似的方法来筛选元素,用到的方法是 soup.select(),返回类型是 list
(1)通过标签名查找
print soup.select('title') #[<title>The Dormouse's story</title>] print soup.select('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>] print soup.select('b')#[<b>The Dormouse's story</b>]
(2)通过类名查找
print soup.select('.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>]
(3)通过 id 名查找
print soup.select('#link1')#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
(4)组合查找
组合查找即和写 class 文件时,标签名与类名、id名进行的组合原理是一样的,例如查找 p 标签中,id 等于 link1的内容,二者需要用空格分开
print soup.select('p #link1')#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
直接子标签查找
print soup.select("head > title")#[<title>The Dormouse's story</title>]
(5)属性查找
查找时还可以加入属性元素,属性需要用中括号括起来,注意属性和标签属于同一节点,所以中间不能加空格,否则会无法匹配到。
print soup.select("head > title")#[<title>The Dormouse's story</title>] print soup.select('a[href="http://example.com/elsie"]')#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
同样,属性仍然可以与上述查找方式组合,不在同一节点的空格隔开,同一节点的不加空格
print soup.select('p a[href="http://example.com/elsie"]')#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
BeautifulSoup支持CSS选择器是令人惊喜的,这大大方便了我们对特定标签的选取。那么接下来就来试验一下。
比如现在我们需要选取搜狗主页中导航栏中导向“知乎”的链接。
分析文档结构大概有这几个比较容易区分的层级特点:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
鉴于如此的结构,我们可以使用类选择器选择“.top-nav”然后使用后代选择其选择“ul”标签和“li”标签,于是整个选择器可以写为“.top-nav ul li”,在代码中实现如下:
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
得到如下输出:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
但里面出现了“网页”这个没有链接到其他地址的项,要如何将之过滤掉呢。可以结合filter函数进行处理,如下:
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
结果中已经过滤掉“网页”这项了:
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
其中filter高阶函数进行过滤时还可以使用正则来进行匹配筛选。
不过一般情况下仅仅使用selector就能比较精确的获取结果集了,因此filter也不用写得很复杂。
- python爬虫:BeautifulSoup 使用select方法的使用
- python爬虫由浅入深3--BeautifulSoup的使用的基本方法
- Python爬虫(2)--BeautifulSoup的使用
- Python使用BeautifulSoup进行爬虫
- python网络爬虫-使用BeautifulSoup
- 简单爬虫python实现02——BeautifulSoup的使用
- 使用python语言结合beautifulsoup编写简单的网络爬虫
- 使用BeautifulSoup实现的图片爬虫
- 使用BeautifulSoup的简单小爬虫
- python的【爬虫】:使用urllib爬取wiki文章,使用beautifulSoup解析html
- python学习(5):python爬虫之BeautifulSoup库的使用
- python学习(6):python爬虫之requests和BeautifulSoup的使用
- 使用requests+beautifulsoup模块实现python网络爬虫功能
- Python爬虫之使用BeautifulSoup解析HTML文本
- python爬虫入门——beautifulsoup初使用
- Python从零开始写爬虫(二)BeautifulSoup库使用
- Python使用urllib库和BeautifulSoup库爬虫总结
- 使用requests+beautifulsoup模块实现python网络爬虫功能
- 执行SQL语句的增删改查
- ERROR 1118 : Row size too large. The maximum row size for the used table type
- TypeError: __init__() takes at least 4 arguments (4 given)
- 润乾报表中参数表单关联过滤异步加载用法
- python-爬虫(1)
- python爬虫:BeautifulSoup 使用select方法的使用
- 搜索引擎关键字智能提示的一种实现
- 二进制中1的个数
- 城市智商的提出,基于互联网云脑的智慧城市发展水平评测研究
- JVM7、8详解及优化
- 数据结构-排序算法1
- Linux系统逻辑卷管理
- 虚拟机中linux(Centos7)的安装及设置
- 递归如何节省时间