Beautiful Soup4学习笔记(四):搜索文档树

Beautiful Soup定义了很多搜索方法,这里着重介绍2个: find() 和 find_all() .其它方法的参数和用法类似,请读者举一反三.

使用 find_all() 类似的方法可以查找到想要查找的文档内容:

过滤器

介绍 find_all() 方法前,先介绍一下过滤器的类型 ,这些过滤器贯穿整个搜索的API.
过滤器可以被用在tag的name种,节点的属性中,字符串中或他们的混合中。

字符串

最简单的过滤器就是字符串。在搜索方法中传入一个字符串参数,Beautiful Soup会查找与字符串完整匹配的内容,下面的例子用于查找文档中所有的标签:

>>> soup.find_all('b') 
[The Dormouse's story]

如果传入字节码参数,Beautiful Soup会当作UTF-8编码,可以传入一段Unicode 编码来避免Beautiful Soup解析编码出错

正则表达式

如果传入正则表达式作为参数,Beautiful Soup会通过正则表达式的 match() 来匹配内容.下面例子中找出所有以b开头的标签,这表示和标签都应该被找到:

>>> import re  
>>> for tag  in soup.find_all(re.compile("^b")):
...     print(tag.name) 
...     
... 
body
b

下面代码找出所有名字中包含”t”的标签:

 >>> for  tag in soup.find_all(re.compile("t")):
...     print(tag.name)  
...     
... 
html
title

列表

如果传入列表参数,Beautiful Soup会将与列表中任一元素匹配的内容返回.下面代码找到文档中所有标签和标签:

>>> soup.find_all(["a","b"]) 
[The Dormouse's story, Elsie, Lacie, Tillie]

True

True可以匹配任何值,下面的代码查找到所有的tag,但是不会返回字符串节点:

>>> for tag in soup.find_all(True): 
...     print(tag.name)   
...     
... 
html
head
title
body
p
b
p
a
a
a
p

方法

如果没有合适过滤器,那么还可以定义一个方法,方法只接受一个元素参数,如果这个方法返回True表示当前元素匹配被找到,如果不是则返回False
下面方法校验了当前元素,如果包含 class 属性却不包含 id 属性,那么将返回 True:

>>> def has_class_but_no_id(tag):
...     return tag.has_attr('class') and  not tag.has_attr('id') 
... 
#将这个方法作为参数传入find_all()方法,将得到所有

标签: >>> soup.find_all(has_class_but_no_id) [

The Dormouse's story

,

Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

,

...

]

注意:并没有生效,改天问个大神看看

通过一个方法来过滤一类标签属性的时候, 这个方法的参数是要被过滤的属性的值, 而不是这个标签. 下面的例子是找出 href 属性不符合指定正则的 a 标签.

>>> def not_lacie(href):
...     return   href and not re.compile("lacie").search(href) 
... 
>>> soup.find_all(href=not_lacie)
[Elsie, Tillie]

标签过滤方法可以使用复杂方法. 下面的例子可以过滤出前后都有文字的标签.

>>> from bs4 import NavigableString
>>> def surrounded_by_strings(tag):
...     return ( isinstance(tag.next_element,NavigableString)  and isinstance(tag.previous_element,NavigableString))
... 
>>> for tag in soup.find_all(surrounded_by_strings): 
...     print(tag.name)   
...     
... 
body
p
a
a
a
p

find_all()

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

>>> soup.find_all('title')
[The Dormouse's story]
>>> soup.find_all("p","title") 
[

The Dormouse's story

] >>> soup.find_all("a") [Elsie, Lacie, Tillie] >>> soup.find_all(id = "link2") [Lacie] >>> import re >>> soup.find(string = re.compile("sisters") ... ) 'Once upon a time there were three little sisters; and their names were\n'

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

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

name参数

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

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

注意:搜索name参数的值可以使用任一类型的过滤器,字符串,正则表达式,列表,方法或是True。

keyword参数

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

>>> soup.find_all(id = "link2")  
[Lacie]

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

>>> soup.find_all(href=re.compile("elsie")) 
[Elsie]

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

>>> soup.find_all(id=True) 
[Elsie, Lacie, Tillie]

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

>>> soup.find_all(href=re.compile("elsie"), id='link1')
[Elsie]

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

>>> data_soup = BeautifulSoup('
foo!
') >>> data_soup.find_all(data-foo="value") File "", line 1 SyntaxError: keyword can't be an expression

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

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

按CSS搜索

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

>>> soup.find_all("a",class_="sister") 
[Elsie, Lacie, Tillie]

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

>>> soup.find_all(class_=re.compile("itl")) 
[

The Dormouse's story

] >>> def has_six_characters(css_class): ... return css_class is not None and len(css_class) == 6 ... >>> soup.find_all(class_=has_six_characters) [Elsie, Lacie, Tillie]

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

>>> css_soup = BeautifulSoup('

') >>> css_soup.find_all("p", class_="strikeout") [

] >>> >>> css_soup.find_all("p", class_="body") [

]

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

>>> css_soup.find_all("p", class_="body strikeout")
[

]

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

>>> soup.find_all("a",attrs={"class":"sister"}) 
[Elsie, Lacie, Tillie]

string参数

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

>>> soup.find_all(string="Elsie")
['Elsie']
>>> 
>>> soup.find_all(string=["Tillie", "Elsie", "Lacie"])
['Elsie', 'Lacie', 'Tillie']
>>> soup.find_all(string=re.compile("Dormouse"))
["The Dormouse's story", "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(string=is_the_only_string_within_a_tag) 
["The Dormouse's story", "The Dormouse's story", 'Elsie', 'Lacie', 'Tillie', '...']

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

>>> soup.find_all("a", string="Elsie") 
[Elsie]

limit参数

find_all() 方法返回全部的搜索结构,如果文档树很大那么搜索会很慢.如果我们不需要全部结果,可以使用 limit 参数限制返回结果的数量.效果与SQL中的limit关键字类似,当搜索到的结果数量达到 limit 的限制时,就停止搜索返回结果.

文档树中有3个tag符合搜索条件,但结果只返回了2个,因为我们限制了返回数量:

>>> soup.find_all("a", limit=2) 
[Elsie, Lacie]

recursive参数

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


 
  
   The Dormouse's story
  
 
...

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

>>> html_doc = """
... 
...  
...   
...    The Dormouse's story
...   
...  
... ...
... """
>>> soup = BeautifulSoup(html_doc, 'html.parser')   
>>> soup.html.find_all("title") 
[
   The Dormouse's story
  ]
>>> soup.html.find_all("title",recursive=False) 
[]

标签在 <html> 标签下, 但并不是直接子节点, <head> 标签才是直接子节点. 在允许查询所有后代节点时 Beautiful Soup 能够查找到 <title> 标签. 但是使用了 recursive=False 参数之后,只能查找直接子节点,这样就查不到 <title> 标签了.</p> <p>Beautiful Soup 提供了多种DOM树搜索方法. 这些方法都使用了类似的参数定义. 比如这些方法: find_all(): name, attrs, text, limit. 但是只有 find_all() 和 find() 支持 recursive 参数.</p> <h2>像调用find_all()一样调用tag</h2> <p>find_all() 几乎是Beautiful Soup中最常用的搜索方法,所以我们定义了它的简写方法. BeautifulSoup 对象和 tag 对象可以被当作一个方法来使用,这个方法的执行结果与调用这个对象的 find_all() 方法相同,下面两行代码是等价的:</p> <pre><code>>>> soup.find_all("a") [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/l acie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] >>> soup("a") [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/l acie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] </code></pre> <p>这两行代码也是等价的:</p> <pre><code>>>> soup.title.find_all(string=True) ["The Dormouse's story"] >>> soup.title(string=True) ["The Dormouse's story"] >>> </code></pre> <h3>find()</h3> <p>find(name,attrs,recursive,string,**kwargs)<br> find_all() 方法将返回文档中符合条件的所有tag,尽管有时候我们只想得到一个结果.比如文档中只有一个<body>标签,那么使用 find_all() 方法来查找<body>标签就不太合适, 使用 find_all 方法并设置 limit=1 参数不如直接使用 find() 方法.下面两行代码是等价的:</p> <pre><code>>>> soup.find_all("title",limit=1) [<title>The Dormouse's story] >>> soup.find("title") The Dormouse's story

唯一的区别是dind_all()方法返回的结果是包含值的元素列表,而find()函数直接返回结果。

find_all() 方法没有找到目标是返回空列表, find() 方法找不到目标时,返回 None .

>>> print(soup.find("nojscnef"))
None

soup.head.title是tag的名字方法的简写,这个简写的原理就是多次调用当前tag的find()方法:

>>> soup.head.title
The Dormouse's story
>>> soup.find("head").find("title") 
The Dormouse's story

find_parents()和find_parent()

find_parents(name,attrs,recursive,string,kwargs)
find_parent(name,attrs,recursive,string,
kwargs)
我们已经用了很大篇幅来介绍 find_all() 和 find() 方法,Beautiful Soup中还有10个用于搜索的API.它们中的五个用的是与 find_all() 相同的搜索参数,另外5个与 find() 方法的搜索参数类似.区别仅是它们搜索文档的不同部分.

注意:find_all()和find()只搜索当前节点的所有子节点,子孙节点等。find_parents()和find_parent()用来搜索当前节点的父辈节点,搜索方法与普通的tag的搜索方法相同,搜索文档包含的内容。我们从一个文档中的一个叶子节点开始:

>>> a_string  = soup.find(string = "Lacie") 
>>> a_string 
'Lacie'
>>> a_string.find_parents("a")
[Lacie]
>>> a_string.find_parent("p") 

Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

>>> a_string.find_parents("p","title") []

文档中的一个标签是当前叶子节点的直接父节点,所以可以被找到.还有一个

标签,是目标叶子节点的间接父辈节点,所以也可以被找到.包含class值为”title”的

标签不是不是目标叶子节点的父辈节点,所以通过 find_parents() 方法搜索不到.

find_parent() 和 find_parents() 方法会让人联想到 .parent 和 .parents 属性.它们之间的联系非常紧密.搜索父辈节点的方法实际上就是对 .parents 属性的迭代搜索.

find_next_siblings()和find_next_sibling()

find_next_siblings( name , attrs , recursive , string , **kwargs )
find_next_sibling( name , attrs , recursive , string , **kwargs )

这2个方法通过 .next_siblings 属性对当tag的所有后面解析 [5] 的兄弟tag节点进行迭代, find_next_siblings() 方法返回所有符合条件的后面的兄弟节点, find_next_sibling()只返回符合条件的后面的第一个tag节点.

>>> first_link = soup.a  
>>> first_link 
Elsie
>>> first_link.find_next_siblings("a") 
[Lacie, Tillie]
>>> first_story_paragraph = soup.find("p","story") 
>>> first_story_paragraph.find_next_sibling("p") 

...

find_previous_siblings() 和 find_previous_sibling()

find_previous_siblings( name , attrs , recursive , string , **kwargs )
find_previou_siblings( name , attrs , recursive , string , **kwargs )

这2个方法通过 .previous_siblings 属性对当前tag的前面解析 [5] 的兄弟tag节点进行迭代, find_previous_siblings() 方法返回所有符合条件的前面的兄弟节点, find_previous_sibling() 方法返回第一个符合条件的前面的兄弟节点:

>>> last_link = soup.find("a",id = "link3") 
>>> last_link 
Tillie
>>> last_link.find_previous_siblings("a") 
[Lacie, Elsie]
>>> first_story_paragraph = soup.find("p", "story")
>>> first_story_paragraph.find_previous_sibling("p")

The Dormouse's story

find_all_next()和find_next()

find_all_next( name , attrs , recursive , string , **kwargs )
find_next( name , attrs , recursive , string , **kwargs )

这2个方法通过 .next_elements 属性对当前tag的之后的 tag和字符串进行迭代, find_all_next() 方法返回所有符合条件的节点, find_next() 方法返回第一个符合条件的节点:

>>> first_link = soup.a 
>>> first_link  
Elsie
>>> first_link.find_all_next(string=True) 
['Elsie', ',\n', 'Lacie', ' and\n', 'Tillie', ';\nand they lived at the bottom of a well.', '\n', '...', '\n']
>>> first_link.find_next("p") 

...

第一个例子中,字符串 “Elsie”也被显示出来,尽管它被包含在我们开始查找的标签的里面.第二个例子中,最后一个

标签也被显示出来,尽管它与我们开始查找位置的标签不属于同一部分.例子中,搜索的重点是要匹配过滤器的条件,并且在文档中出现的顺序而不是开始查找的元素的位置.

find_all_previous()和find_previous()

find_all_previous( name , attrs , recursive , string , **kwargs )
find_previous( name , attrs , recursive , string , **kwargs )

这2个方法通过 .previous_elements 属性对当前节点前面 的tag和字符串进行迭代, find_all_previous() 方法返回所有符合条件的节点, find_previous() 方法返回第一个符合条件的节点.

>>> first_link = soup.a 
>>> first_link 
Elsie
>>> first_link.find_all_previous("p")
[

Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

,

The Dormouse's story

]

find_all_previous("p") 返回了文档中的第一段(class=”title”的那段),但还返回了第二段,

标签包含了我们开始查找的标签.不要惊讶,这段代码的功能是查找所有出现在指定标签之前的

标签,因为这个

标签包含了开始的标签,所以

标签一定是在之前出现的.

CSS选择器

Beautiful Soup支持大部分的CSS选择器,在Tag或BeautifSoup对象的.select()方法中传入字符串参数,既可以使用CSS选择器的语法找到tag:

>>> soup.select("title") 
[The Dormouse's story]

通过tag标签逐层查找:

>>> soup.select("body a")
[Elsie, Lacie, Tillie]
>>> soup.select("html head title") 
[The Dormouse's story]

找到某个tag标签下的直接子标签:

>>> soup.select("head > title") 
[The Dormouse's story]
>>> soup.select("p > a") 
[Elsie, Lacie, Tillie]
>>> soup.select("p > #link1") 
[Elsie]
>>> soup.select("body > a") 
[]
>>> soup.select("p > a:nth-of-type(2)") 
[Lacie]

找到兄弟节点:

>>> soup.select("#link1 ~ .sister") 
[Lacie, Tillie]
>>> soup.select("#link1 + .sister") 
[Lacie]

通过CSS的类名查找:

>>> soup.select(".sister") 
[Elsie, Lacie, Tillie]

>>> soup.select("[class~=sister]")
[Elsie, Lacie, Tillie]

通过tag的id查找:

>>> soup.select("#link1")
[Elsie]
>>> soup.select("a#link1")
[Elsie]

同时用多种CSS选择器查询元素:

>>> soup.select("#link1,#link2") 
[Elsie, Lacie]

通过是否存在某个属性来查找:

>>> soup.select("a[href]")
[Elsie, Lacie, Tillie]

通过属性的值来查找:

>>> soup.select('a[href="http://example.com/elsie"]') 
[Elsie]
>>> soup.select('a[href^="http://example.com/"]') 
[Elsie, Lacie, Tillie]
>>> soup.select('a[href$="tillie"]') 
[Tillie]
>>> soup.select('a[href*=".com/el"]') 
[Elsie]

通过语言设置来查找:

>>> multilingual_markup = """
...  

Hello

...

Howdy, y'all

...

Pip-pip, old fruit

...

Bonjour mes amis

... """ >>> multilingual_soup = BeautifulSoup(multilingual_markup) >>> multilingual_soup.select('p[lang|=en]') [

Hello

,

Howdy, y'all

,

Pip-pip, old fruit

]

返回查找到的元素的第一个

>>> soup.select_one(".sister") 
Elsie

你可能感兴趣的:(Beautiful Soup4学习笔记(四):搜索文档树)