BeautifulSoup安装以及简单抓取
beautifulsoup4.4.0官方文档:
使用实例网站:bilibli.com
https://beautifulsoup.readthedocs.io/zh_CN/latest/#id7
操作系统:Ubuntu20.04LTS
一、beautifulsoup安装
$ apt-get install Python-bs4
$ pip install beautifulsoup
在此之前需要安装python,requests库以及pip
$sudo apt install requests
$ sudo apt install python3
$ sudo apt install pip
二、beautifulsoup基本命令
1、首先import一下需要的模块
$import requests
$from bs4 import BeautifulSoup
2、创建soup对象
soup = BeautifulSoup(url)
3、输出soup对象
soup.title
#
The Dormouse's storysoup.title.name
# u'title'soup.title.string# u'The Dormouse's story'soup.title.parent.name
# u'head'soup.p
#
The Dormouse's story
soup.p['class']
# u'title'soup.a
#Elsiesoup.find_all('a')
# [Elsie,
#Lacie,
#Tillie]
soup.find(id="link3")
4、遍历文件树
$r = soup.contents
$print (r)
5、搜索文档树
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 importBeautifulSoupimportre
file= open('./aa.html', 'rb')
html=file.read()
bs= BeautifulSoup(html,"html.parser")
t_list= bs.find_all(re.compile("a"))for item int_list:print(item)
列表:如果传入一个列表,BeautifulSoup4将会与列表中的任一元素匹配到的节点返回
t_list = bs.find_all(["meta","link"])for item int_list:print(item)
方法:传入一个方法,根据方法来匹配
from bs4 importBeautifulSoup
file= open('./aa.html', 'rb')
html=file.read()
bs= BeautifulSoup(html,"html.parser")defname_is_exists(tag):return tag.has_attr("name")
t_list=bs.find_all(name_is_exists)for item int_list:print(item)
(2)kwargs参数:
from bs4 importBeautifulSoupimportre
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 int_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 int_list:print(item)
(4)text参数:
通过text参数可以搜索文档中的字符串内容,与name参数的可选值一样,text参数接受 字符串,正则表达式,列表
from bs4 importBeautifulSoupimportre
file= open('./aa.html', 'rb')
html=file.read()
bs= BeautifulSoup(html, "html.parser")
t_list= bs.find_all(attrs={"data-foo": "value"})for item int_list:print(item)
t_list= bs.find_all(text="hao123")for item int_list:print(item)
t_list= bs.find_all(text=["hao123", "地图", "贴吧"])for item int_list:print(item)
t_list= bs.find_all(text=re.compile("\d"))for item int_list:print(item)
当我们搜索text中的一些特殊属性时,同样也可以传入一个方法来达到我们的目的:
deflength_is_two(text):return text and len(text) == 2t_list= bs.find_all(text=length_is_two)for item int_list:print(item)
(5)limit参数:
可以传入一个limit参数来限制返回的数量,当搜索出的数据量为5,而设置了limit=2时,此时只会返回前2个数据
from bs4 importBeautifulSoupimportre
file= open('./aa.html', 'rb')
html=file.read()
bs= BeautifulSoup(html, "html.parser")
t_list= bs.find_all("a",limit=2)for item int_list:print(item)