爬虫之BeautifulSoup, CSS
1. Beautiful Soup的简介
2. Beautiful Soup 安装
可以利用 pip 或者 easy_install 来安装,以下两种方法均可
easy_install beautifulsoup4
4. 创建 Beautiful Soup 对象
首先必须要导入 bs4 库:from bs4 import BeautifulSoup
我们创建一个字符串,后面的例子我们便会用它来演示
另外,我们还可以用本地 HTML 文件来创建对象,例如soup = BeautifulSoup(open('index.html'))
上面这句代码便是将本地 index.html 文件打开,用它来创建 soup 对象
下面我们来打印一下 soup 对象的内容,格式化输出 :print soup.prettify()
5. 四大对象种类
Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种:
- Tag
- NavigableString
- BeautifulSoup
- Comment
Tag 是什么?通俗点讲就是 HTML 中的一个个标签,例如:<title>The Dormouse's story</title> ;<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
上面的 title a 等等 HTML 标签加上里面包括的内容就是 Tag,下面我们来感受一下怎样用 Beautiful Soup 来方便地获取 Tags
对于 Tag,它有两个重要的属性,是 name 和 attrs,下面我们分别来感受一下
在这里,我们把 p 标签的所有属性打印输出了出来,得到的类型是一个字典。
如果我们想要单独获取某个属性,可以这样,例如我们获取它的 class 叫什么
print soup.p['class']
(2)NavigableString
既然我们已经得到了标签的内容,那么问题来了,我们要想获取标签内部的文字怎么办呢?很简单,用 .string 即可,例如:
print soup.p.string
#The Dormouse's story
(3)BeautifulSoup
BeautifulSoup 对象表示的是一个文档的全部内容.大部分时候,可以把它当作 Tag 对象,是一个特殊的 Tag,我们可以分别获取它的类型,名称,以及属性来感受一下
print type(soup.name)
(4)Comment
Comment 对象是一个特殊类型的 NavigableString 对象,其实输出的内容仍然不包括注释符号,但是如果不好好处理它,可能会对我们的文本处理造成意想不到的麻烦。
我们找一个带注释的标签:
print soup.a
7.搜索文档树
(1)find_all( name , attrs , recursive , text , **kwargs )
(2)find( name , attrs , recursive , text , **kwargs )
它与 find_all() 方法唯一的区别是 find_all() 方法的返回结果是值包含一个元素的列表,而 find() 方法直接返回结果
find_all( name , attrs , recursive , text , **kwargs )
find_all() 方法搜索当前tag的所有tag子节点,并判断是否符合过滤器的条件
1)name 参数
name 参数可以查找所有名字为 name 的tag,字符串对象会被自动忽略掉
A.传字符串
最简单的过滤器是字符串.在搜索方法中传入一个字符串参数,Beautiful Soup会查找与字符串完整匹配的内容,下面的例子用于查找文档中所有的<b>标签
B.传正则表达式
如果传入正则表达式作为参数,Beautiful Soup会通过正则表达式的 match() 来匹配内容.下面例子中找出所有以b开头的标签,这表示<body>和<b>标签都应该被找到
如果传入列表参数,Beautiful Soup会将与列表中任一元素匹配的内容返回.下面代码找到文档中所有<a>标签和<b>标签
True 可以匹配任何值,下面代码查找到所有的tag,但是不会返回字符串节点
如果没有合适过滤器,那么还可以定义一个方法,方法只接受一个元素参数 [4] ,如果这个方法返回 True 表示当前元素匹配并且被找到,如果不是则反回 False
下面方法校验了当前元素,如果包含 class 属性却不包含 id 属性,那么将返回 True:
使用多个指定名字的参数可以同时过滤tag的多个属性
在这里我们想用 class 过滤,不过 class 是 python 的关键词,这怎么办?加个下划线就可以
有些tag属性在搜索不能使用,比如HTML5中的 data-* 属性
但是可以通过 find_all() 方法的 attrs 参数定义一个字典参数来搜索包含特殊属性的tag
3)text 参数
通过 text 参数可以搜搜文档中的字符串内容.与 name 参数的可选值一样, text 参数接受 字符串 , 正则表达式 , 列表, True
4)limit 参数
find_all() 方法返回全部的搜索结构,如果文档树很大那么搜索会很慢.如果我们不需要全部结果,可以使用 limit 参数限制返回结果的数量.效果与SQL中的limit关键字类似,当搜索到的结果数量达到 limit 的限制时,就停止搜索返回结果.
文档树中有3个tag符合搜索条件,但结果只返回了2个,因为我们限制了返回数量
5)recursive 参数
调用tag的 find_all() 方法时,Beautiful Soup会检索当前tag的所有子孙节点,如果只想搜索tag的直接子节点,可以使用参数 recursive=False .
一段简单的文档:
8.CSS选择器
我们在写 CSS 时,标签名不加任何修饰,类名前加点,id名前加 #,在这里我们也可以利用类似的方法来筛选元素,用到的方法是 soup.select(),返回类型是 list
(1)通过标签名查找
print soup.select('title')
#[<title>The Dormouse's story</title>]
(2)通过类名查找
print soup.select('.sister')
组合查找即和写 class 文件时,标签名与类名、id名进行的组合原理是一样的,例如查找 p 标签中,id 等于 link1的内容,二者需要用空格分开
直接子标签查找
(5)属性查找
查找时还可以加入属性元素,属性需要用中括号括起来,注意属性和标签属于同一节点,所以中间不能加空格,否则会无法匹配到。
同样,属性仍然可以与上述查找方式组合,不在同一节点的空格隔开,同一节点的不加空格
最新文章
- <;精通JavaScript>;---阅读笔记01
- php代码小实例
- 抛弃NVelocity,来玩玩Razor
- pycharm的安装
- php数组分页类
- django学习之Model(五)MakingQuery
- 读TIJ -2 一切都是对象
- python:os.path
- linux下keepalived 安装配置
- 6.Java集合总结系列:常见集合类的使用(List/Set/Map)
- java中继承和组合的区别
- 解决 AutoMapper ProjectTo 不起作用的问题
- Oracle误删除数据和表的恢复办法包括truncate
- py-day4 python filter函数
- uwsgi加nginx部署django restframework前后端分离项目
- 无线投屏PC投电视
- Python爬虫-request的用法
- Python3 格式化输出 %s &; %d 等
- POJ 2891 中国剩余定理的非互质形式
- Missy