pythonisbeautiful_python-beautifulsoup菜鸟教程
findtable = mysoup.find('table', attrs={'class': 'GridTableContent'})tbody = table.find('tbody')
find_all 和 find ⽤法类似
trList = tbody.find_all('tr')
菜鸟教程python面向对象六、搜索⽂档树
6.1、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 = ad()
bs = BeautifulSoup(html,"html.parser")
t_list = bs.find_all(repile("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 = ad()
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 = ad()
bs = BeautifulSoup(html,"html.parser")
# 查询id=head的Tag
t_list = bs.find_all(id="head") print(t_list)
# 查询href属性包含ss1.bdstatic的Tag
t_list = bs.find_all(href=repile("news.baidu"))
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 = ad()
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=repile("\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 = ad()
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="新闻")
6.2、find()
find()将返回符合条件的第⼀个Tag,有时我们只需要或⼀个Tag时,我们就可以⽤到find()⽅法了。当然了,也可以使⽤find_all()⽅法,传⼊⼀个limit=1,然后再取出第⼀个值也是可以的,不过未免繁琐。from bs4 import BeautifulSoup
import re
file = open('./aa.html', 'rb')
html = ad()
bs = BeautifulSoup(html, "html.parser")
# 返回只有⼀个结果的列表
t_list = bs.find_all("title",limit=1)
print(t_list)
# 返回唯⼀值
t = bs.find("title")
print(t)
# 如果没有到,则返回None
t = bs.find("abc") print(t)
从结果可以看出find_all,尽管传⼊了limit=1,但是返回值仍然为⼀个列表,当我们只需要取⼀个值时,远不如find⽅法⽅便。但是如果未搜索到值时,将返回⼀个None
在上⾯介绍BeautifulSoup4的时候,我们知道可以通过bs.div来获取第⼀个div标签,如果我们需要获取第⼀个div下的第⼀个div,我们可以这样:t = bs.div.div
# 等价于
t = bs.find("div").find("div")

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。