find_all()

find_all( name , attrs , recursive , text , **kwargs )
find_all() 方法搜索当前tag的所有tag子节点,并判断是否符合过滤器的条件.这里有几个例子:

soup.find_all("title")
#[<title>The Dormouse's story</title>]soup.find_all("p", "title")
#[<p class="title"><b>The Dormouse's story</b></p>]soup.find_all("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.find_all(id="link2")
#[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]import re
soup.find(text=re.compile("sisters"))
#u'Once upon a time there were three little sisters; and their names were\n'

有几个方法很相似,还有几个方法是新的,参数中的 text 和 id 是什么含义? 为什么 find_all(“p”, “title”) 返回的是CSS Class为”title”的

标签? 我们来仔细看一下 find_all() 的参数

name 参数

name 参数可以查找所有名字为 name 的tag,字符串对象会被自动忽略掉.
简单的用法如下:

soup.find_all("title")
#[<title>The Dormouse's story</title>]

重申: 搜索 name 参数的值可以使任一类型的 过滤器 ,字符窜,正则表达式,列表,方法或是 True .

keyword 参数

如果一个指定名字的参数不是搜索内置的参数名,搜索时会把该参数当作指定名字tag的属性来搜索,如果包含一个名字为 id 的参数,Beautiful Soup会搜索每个tag的”id”属性.

soup.find_all(id='link2')
#[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

如果传入 href 参数,Beautiful Soup会搜索每个tag的”href”属性:

soup.find_all(href=re.compile("elsie"))
#[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]

搜索指定名字的属性时可以使用的参数值包括 字符串 , 正则表达式 , 列表, True .
下面的例子在文档树中查找所有包含 id 属性的tag,无论 id 的值是什么:

soup.find_all(id=True)
#[<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>]

使用多个指定名字的参数可以同时过滤tag的多个属性:

soup.find_all(href=re.compile("elsie"), id='link1')
#[<a class="sister" href="http://example.com/elsie" id="link1">three</a>]

有些tag属性在搜索不能使用,比如HTML5中的 data-* 属性:

data_soup = BeautifulSoup('<div data-foo="value">foo!</div>')
data_soup.find_all(data-foo="value")
#SyntaxError: keyword can't be an expression

但是可以通过 find_all() 方法的 attrs 参数定义一个字典参数来搜索包含特殊属性的tag:

data_soup.find_all(attrs={"data-foo": "value"})
#[<div data-foo="value">foo!</div>]

按CSS搜索

按照CSS类名搜索tag的功能非常实用,但标识CSS类名的关键字 class 在Python中是保留字,使用 class 做参数会导致语法错误.从Beautiful Soup的4.1.1版本开始,可以通过 class_ 参数搜索有指定CSS类名的tag:

soup.find_all("a", 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>]

class_ 参数同样接受不同类型的 过滤器 ,字符串,正则表达式,方法或 True :

 soup.find_all(class_=re.compile("itl"))#[<p class="title"><b>The Dormouse's story</b></p>]def has_six_characters(css_class):return css_class is not None and len(css_class) == 6soup.find_all(class_=has_six_characters)#[<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>]

tag的 class 属性是 多值属性 .按照CSS类名搜索tag时,可以分别搜索tag中的每个CSS类名:

css_soup = BeautifulSoup('<p class="body strikeout"></p>')
css_soup.find_all("p", class_="strikeout")
#[<p class="body strikeout"></p>]css_soup.find_all("p", class_="body")
#[<p class="body strikeout"></p>]

搜索 class 属性时也可以通过CSS值完全匹配:

css_soup.find_all("p", class_="body strikeout")
#[<p class="body strikeout"></p>]

完全匹配 class 的值时,如果CSS类名的顺序与实际不符,将搜索不到结果:

soup.find_all("a", attrs={"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>]

text 参数

通过 text 参数可以搜搜文档中的字符串内容.与 name 参数的可选值一样, text 参数接受 字符串 , 正则表达式 , 列表, True . 看例子:

soup.find_all(text="Elsie")
#[u'Elsie']soup.find_all(text=["Tillie", "Elsie", "Lacie"])
#[u'Elsie', u'Lacie', u'Tillie']soup.find_all(text=re.compile("Dormouse"))
[u"The Dormouse's story", u"The Dormouse's story"]def is_the_only_string_within_a_tag(s):""Return True if this string is the only child of its parent tag.""return (s == s.parent.string)soup.find_all(text=is_the_only_string_within_a_tag)
#[u"The Dormouse's story", u"The Dormouse's story", u'Elsie', u'Lacie', u'Tillie', u'...']

虽然 text 参数用于搜索字符串,还可以与其它参数混合使用来过滤tag.Beautiful Soup会找到 .string 方法与 text 参数值相符的tag.下面代码用来搜索内容里面包含“Elsie”的标签:

soup.find_all("a", text="Elsie")
#[<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>]

limit 参数

find_all() 方法返回全部的搜索结构,如果文档树很大那么搜索会很慢.如果我们不需要全部结果,可以使用 limit 参数限制返回结果的数量.效果与SQL中的limit关键字类似,当搜索到的结果数量达到 limit 的限制时,就停止搜索返回结果.
文档树中有3个tag符合搜索条件,但结果只返回了2个,因为我们限制了返回数量:

soup.find_all("a", limit=2)
#[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
#<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

recursive 参数

调用tag的 find_all() 方法时,Beautiful Soup会检索当前tag的所有子孙节点,如果只想搜索tag的直接子节点,可以使用参数 recursive=False .
一段简单的文档:

<html><head><title>The Dormouse's story</title></head>
...

是否使用 recursive 参数的搜索结果:

soup.html.find_all("title")
#[<title>The Dormouse's story</title>]soup.html.find_all("title", recursive=False)
#[]

像调用 find_all() 一样调用tag
find_all() 几乎是Beautiful Soup中最常用的搜索方法,所以我们定义了它的简写方法. BeautifulSoup 对象和 tag 对象可以被当作一个方法来使用,这个方法的执行结果与调用这个对象的 find_all() 方法相同,下面两行代码是等价的:

soup.find_all("a")
soup("a")

这两行代码也是等价的:

soup.title.find_all(text=True)
soup.title(text=True)

欢迎关注,一起学习。有用点个赞吧!
公众号:一个甜甜的大橙子
知乎:一个甜甜的大橙子

【Python爬虫】Beautifulsoup4中find_all函数相关推荐

  1. Python使用numpy中trim_zeros函数去除首尾0值的语法

    Python使用numpy中trim_zeros函数去除首尾0值的语法 目录 Python使用numpy中trim_zeros函数去除首尾0值的语法 #numpy中trim_zeros

  2. Python class 类中 __init__ 函数

    什么是类? 类 是通过执行类语句创建的特定对象类型.类对象 被当作模板来创建实例对象,实例对象包含了特定于数据类型的数据(属性)和代码(方法). 类可以基于一个或多个的其他类,称之为基类(ES),它继 ...

  3. Python爬虫过程中验证码识别的三种解决方案

    在Python爬虫过程中,有些网站需要验证码通过后方可进入网页,目的很简单,就是区分是人阅读访问还是机器爬虫.验证码问题看似简单,想做到准确率很高,也是一件不容易的事情.为了更好学习爬虫,后续推文中将 ...

  4. [转载] python 列表List中index函数的坑

    参考链接: Python列表list sort() python 列表List中index函数的坑 例如 a = [1, 2, 1] 如果使用 a.index(1), 输出的只是列表中第一个出现的 1 ...

  5. fib函数用python编写_Python中利用函数装饰器实现备忘功能

    "备忘"的定义 "memoization"(备忘)这个词是由Donald Michie在1968年提出的,它基于拉丁语单词"memorandum&qu ...

  6. Python 同一个类中不同函数相互调用

    本文采取:Python的类中函数方法相互调用的两种方式_狗哥的博客-CSDN博客 __metaclass__=type class Stu:name=Noneage=Noneschool=" ...

  7. Python 3.x中reduce()函数完整用法

    在Python 3.x中,reduce()不再是内置函数,而是移到了标准库functools中,需要先导入再使用,其官方解释如图所示: 从官方文档可知,该函数用法要点如下: 1)把一个双参数函数以累积 ...

  8. 在python的解释器中使用函数_浅析Windows 嵌入python解释器的过程

    这次主要记录在windows下嵌入 python 解释器的过程,程序没有多少,主要是头文件与库文件的提取. 程序平台:windows10 64 bit. Qt 5.5.1  MSVC  2013 32 ...

  9. python里endswith_Python中endswith()函数的基本使用

    Python中endswith()函数的基本使用 函数:endswith() 作用:判断字符串是否以指定字符或子字符串结尾,常用于判断文件类型 相关函数:判断字符串开头 startswith() 一. ...

最新文章

  1. 数位DP 回文序列 POJ-3280 Cheapest Palindrome
  2. 人生抉择之-创业记录片(一)
  3. 服务器安全防护和保护措施方案—Vecloud
  4. java的多态性学习代码
  5. SQL Server-外部联接基础回顾(十三)
  6. frame,iframe,frameset 的区别
  7. OSChina 周六乱弹 —— 这个版本的小红帽听说过吗?
  8. 怎么学习正则表达式?(正则的使用心得)
  9. 程序如何在两个gpu卡上并行运行_深度学习分布式训练相关介绍 - Part 1 多GPU训练...
  10. 复制含有随机指针节点的链表~哈希表的使用~(⌒▽⌒)
  11. DBA一族九阳神功秘籍
  12. 农行支付php,ECSHOP教程:农行支付接口开发(含手机端)
  13. 移动端高清适配方案(解决图片模糊问题、1px细线问题)
  14. mouseover显示层mouseout隐藏层,并且在鼠标放上层时显示层【原】
  15. Windows 使用VMWare虚拟机安装黑苹果
  16. Hydra/Medusa爆破工具命令参数和简单操作
  17. 周跳探测——历元间差分法
  18. 如何将Mac设置为热点?
  19. 双目立体匹配修炼之路
  20. springcloud(一)微服务概述

热门文章

  1. iic获取salve设备地址_Linux下使用IIC总线读写EEPROM(读写i2c从设备通用程序)
  2. 画手cv什么意思_b站cv和op是什么意思
  3. 名悦集团分享纯电动汽车保养的几个小秘诀
  4. calltransaction弹出新的窗口_SAP刘梦_新浪博客
  5. Node.js 单页应用
  6. python绘制玫瑰曲线_数学的有趣图形-玫瑰线
  7. java安全学习(一)
  8. 【Benewake(北醒) 】长距 TF350 350m介绍以及资料整理
  9. IntelliJ IDEA快速入门 | 第二十八篇:详述IntelliJ IDEA(或者Eclipse)中一些常用的快捷键(下)
  10. mysql8 中的rank_Mysql8.0+中的rank()、row_num()、dense_rank()等窗口函数