python网络爬虫开发:BeautifulSoup遍历、搜素文档树
1. 遍历文档树
在解析文档文件的过程中,如果需要遍历文档,我们需要使用到一些特殊的方法,
例如:
1) .contents
获取Tag的所有子节点,以列表的形式返回
例如:
|
1
2
3
|
con = soup.head.contentsfor i in con: print(i) |
先把tag的.content对象以列表的形式存储在con中,然后通过遍历来查看其中的元素。
2) .children
获取Tag的所有子节点,存储在一个生成器中,可以直接通过遍历的方式来访问,和上面例子一致。
3) .descendants
获取Tag的所有子孙节点。
4) .strings
获取子孙节点中的所有内容,可以通过遍历的方式来访问。
5) .parent
获取到Tag标签的父节点。
6) .parents
递归得到所有父辈节点,存放在一个生成器中,可以通过遍历的方式来访问。
7) .previous_siblings
获取Tag上面的所有兄弟节点,返回生成器。
8) .next_siblings
获取Tag下面的所有兄弟节点,返回生成器。
9) .has_attr
用于判断Tag是否包含属性。
2. 搜素文档树
我们在使用的过程中如果要匹配到搜索内容的全部信息,这时候就需要搜索整个文档树,我们需要采用到find_all方法,这个过滤器能贯穿整个搜索的AIP,它可以使用在tag的name中,它的语法格式为:
|
1
|
find_all( name , attrs , recursive , text , **kwargs ) |
name参数即tag的名字,attrs为类或id,recursive为递归性,text为文本参数。
我们对于下面一段网页信息进行学习使用。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>C语言网</title></head><body><div id = "contnt"> <div id="top"> <div id="dot"> <a class="dotcpp" href="http://news.baidu.com" name="jc">教程</a> <a class="dotcpp" href="https://www.dotcpp.com/wp/" name="zy">资源</a> <a class="dotcpp" href="https://blog.dotcpp.com/" name="bk">博客</a> <a class="dotcpp" href="https://www.dotcpp.com/team/" name="xz">小组 </a> <a class="dotcpp" href="https://www.dotcpp.com/wp/#" name="xl">训练 </a> <a class="dotcpp" href="https://www.dotcpp.com/oj/contest.html" name="bs">比赛 </a> </div> </div></div></body></html> |
1) name
name对应的是标签的名字,我们可以通过函数判断名字的存在,来遍历文档中的所有name标签的相关信息。
|
1
2
3
4
5
6
7
|
from bs4 import BeautifulSoupsoup = BeautifulSoup(html,"html.parser")def name_is_exists(tag): return tag.has_attr("name")s = soup.find_all(name_is_exists)for i in s: print(i) |
输出结果为:
|
1
2
3
4
5
6
|
<a class="dotcpp" href="http://news.baidu.com" name="jc">教程</a><a class="dotcpp" href="https://www.dotcpp.com/wp/" name="zy">资源</a><a class="dotcpp" href="https://blog.dotcpp.com/" name="bk">博客</a><a class="dotcpp" href="https://www.dotcpp.com/team/" name="xz">小组 </a><a class="dotcpp" href="https://www.dotcpp.com/wp/#" name="xl">训练 </a><a class="dotcpp" href="https://www.dotcpp.com/oj/contest.html" name="bs">比赛 </a> |
通过这个简单的例子我们筛选出了具备name名字的所有标签并输出。
2) attrs
因为在一个html文档中有很多属性,我们想要筛选到我们想要的信息,也可以通过属性的不同来筛选,我们如果把最后文档中最后一个比赛的中的class内容改为:
|
1
|
<a href="https://www.dotcpp.com/oj/contest.html" name="bs" >比赛 </a> |
然后我们通过标签的选择,可以直接获取到这条标签的内容,代码如下:
|
1
2
3
4
5
|
from bs4 import BeautifulSoupsoup = BeautifulSoup(html,"html.parser")s = soup.find_all(attrs={"class":"dot"})for item in s: print(item) |
输出结果为:
|
1
|
<a class="dot" href="https://www.dotcpp.com/oj/contest.html" name="bs">比赛 </a> |
3) Text
Text参数可以搜索文档中的字符串内容,与name参数的可选值一致,也可以通过正则表达式筛选,我们继续对上面的html文件进行过滤,代码如下:
|
1
2
3
4
5
|
from bs4 import BeautifulSoupsoup = BeautifulSoup(html,"html.parser")t_list = soup.find_all(text=["比赛", "博客", "训练"])for item in t_list: print(item) |
输出结果为:
|
1
2
3
|
博客训练比赛 |
在使用的时候还会使用到find()方法,它会返回符合条件的第一个Tag,我们在筛选一条符合要求的信息时可以采用find()方法,当然我们也可以在find_all()中通过limit 参数来限定为‘1’来进行查找,如下面例子中,它们对应的是同一信息。
|
1
2
3
4
|
ts = soup.find_all("title",limit=1) print(ts) t = soup.find("title") print(t) |
3. 总结
BeautifulSoup在我们解析网页的时候简单好用,能够帮助我们快速的找到相应的信息,相应的语法也比较基础,常用的一些内容我们基本介绍完了,如果想要了解更多内容可以通过相关文档进一步的学习。







