异常500,程序异常,Exception
当前位置:Exception500 » Python » Python Beautifulsoup搜索文档树find_all

Python Beautifulsoup搜索文档树find_all

来源:exception500.com    发布时间:2021-05-25 08-33-29    浏览次数:970次

简介:接下来介绍find_all()函数,首先要明确find_dall总返回一个列表,如果没有找到则返回空:BeautifulSoup提供了强大的搜索函数find 和findall,这里的两个方法(findAll和 find)仅对Tag对象以及,顶层剖析对象有效。其标准引用形式为:findAll(name, attrs, recursive, text, limit, **kwargs)。

findall.png

搜索文档树:find_all(name, attrs, recursive, text, **kwargs)

在上面的栗子中我们简单介绍了find_all的使用,接下来介绍一下find_all的更多用法-过滤器。这些过滤器贯穿整个搜索API,过滤器可以被用在tag的name中,节点的属性等。

(1)name参数:

字符串过滤:会查找与字符串完全匹配的内容

a_list = bs.find_all("a")
print(a_list)

正则表达式过滤:如果传入的是正则表达式,那么BeautifulSoup4会通过search()来匹配内容

from bs4 import BeautifulSoup 
import re 
file = open('./aa.html', 'rb') 
html = file.read() 
bs = BeautifulSoup(html,"html.parser") 
t_list = bs.find_all(re.compile("a")) 
for item in t_list: 
   print(item)

列表:如果传入一个列表,BeautifulSoup4将会与列表中的任一元素匹配到的节点返回

t_list = bs.find_all(["meta","link"])
for item in t_list:
    print(item)

方法:传入一个方法,根据方法来匹配

from bs4 import BeautifulSoup 
file = open('./aa.html', 'rb') 
html = file.read() 
bs = BeautifulSoup(html,"html.parser") 
def name_is_exists(tag): 
    return tag.has_attr("name") 
t_list = bs.find_all(name_is_exists) 
for item in t_list: 
    print(item)

(2)kwargs参数:

from bs4 import BeautifulSoup 
import re 
file = open('./aa.html', 'rb') 
html = file.read() 
bs = BeautifulSoup(html,"html.parser") 
# 查询id=head的Tag
t_list = bs.find_all(id="head") print(t_list) 
# 查询href属性包含ss1.bdstatic.com的Tag
t_list = bs.find_all(href=re.compile("http://news.baidu.com")) 
print(t_list) 
# 查询所有包含class的Tag(注意:class在Python中属于关键字,所以加_以示区别)
t_list = bs.find_all(class_=True) 
for item in t_list: 
    print(item)

(3)attrs参数:

并不是所有的属性都可以使用上面这种方式进行搜索,比如HTML的data-*属性:

t_list = bs.find_all(data-foo="value")

如果执行这段代码,将会报错。我们可以使用attrs参数,定义一个字典来搜索包含特殊属性的tag:

t_list = bs.find_all(attrs={"data-foo":"value"})
for item in t_list:
    print(item)

(4)text参数:

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

from bs4 import BeautifulSoup 
import re 
file = open('./aa.html', 'rb') 
html = file.read() 
bs = BeautifulSoup(html, "html.parser") 
t_list = bs.find_all(attrs={"data-foo": "value"}) 
for item in t_list: 
    print(item) 
t_list = bs.find_all(text="hao123") 
for item in t_list: 
    print(item) 
t_list = bs.find_all(text=["hao123", "地图", "贴吧"]) 
for item in t_list: 
    print(item) 
t_list = bs.find_all(text=re.compile("\d")) 
for item in t_list: 
    print(item)

当我们搜索text中的一些特殊属性时,同样也可以传入一个方法来达到我们的目的:

def length_is_two(text):
    return text and len(text) == 2
t_list = bs.find_all(text=length_is_two)
for item in t_list:
    print(item)

(5)limit参数:

可以传入一个limit参数来限制返回的数量,当搜索出的数据量为5,而设置了limit=2时,此时只会返回前2个数据

from bs4 import BeautifulSoup 
import re 
file = open('./aa.html', 'rb') 
html = file.read() 
bs = BeautifulSoup(html, "html.parser") 
t_list = bs.find_all("a",limit=2) 
for item in t_list: 
    print(item)

find_all除了上面一些常规的写法,还可以对其进行一些简写:

# 两者是相等的
# t_list = bs.find_all("a") => t_list = bs("a")
t_list = bs("a") # 两者是相等的
# t_list = bs.a.find_all(text="新闻") => t_list = bs.a(text="新闻")
t_list = bs.a(text="新闻")

完。


[关键词: PythonBeautifulsoupfind_all ]

软件开发 程序错误 异常 500错误 Exception Copyright© 2019-2021  Exception500 版权所有  【蜀ICP备15020376号-9】  网站地图