上海设计网站大全,北京建设高端网站的,有什么免费建站网站,武威建设银行网站文章目录一. BeautifulSoup4二. 解析实例三. 四大对象种类1. Tag2. NavigableString3. BeautifulSoup4. Comment四. 遍历文档树1.直接子节点 #xff1a;.contents .children 属性1). .contents2). .children2. 所有子孙节点: .descendants 属性3. 节点内容: .string 属性五. …
文章目录一. BeautifulSoup4二. 解析实例三. 四大对象种类1. Tag2. NavigableString3. BeautifulSoup4. Comment四. 遍历文档树1.直接子节点 .contents .children 属性1). .contents2). .children2. 所有子孙节点: .descendants 属性3. 节点内容: .string 属性五. 搜索文档树1. find_all(name, attrs, recursive, text, **kwargs)1). name 参数2). keyword 参数3).text 参数2. CSS选择器1). 通过标签名查找2). 通过类名查找3).通过 id 名查找4).组合查找5). 属性查找6). 获取内容一. BeautifulSoup4
官方文档http://beautifulsoup.readthedocs.io/zh_CN/v4.4.0
和 lxml 一样Beautiful Soup 也是一个HTML/XML的解析器主要的功能也是如何解析和提取 HTML/XML 数据。
lxml 只会局部遍历而Beautiful Soup 是基于HTML DOM的会载入整个文档解析整个DOM树因此时间和内存开销都会大很多所以性能要低于lxml。
BeautifulSoup 用来解析 HTML 比较简单API非常人性化支持CSS选择器、Python标准库中的HTML解析器也支持 lxml 的 XML解析器。
Beautiful Soup 3 目前已经停止开发推荐现在的项目使用Beautiful Soup 4。使用 pip 安装即可pip install beautifulsoup4 二. 解析实例
首先必须要导入 bs4 库
from bs4 import BeautifulSouphtml
htmlheadtitleThe Dormouses story/title/head
body
p classtitle namedromousebThe Dormouses story/b/p
p classstoryOnce upon a time there were three little sisters; and their names were
a hrefhttp://example.com/elsie classsister idlink1!-- Elsie --/a,
a hrefhttp://example.com/lacie classsister idlink2Lacie/a and
a hrefhttp://example.com/tillie classsister idlink3Tillie/a;
and they lived at the bottom of a well./p
p classstory.../p
#创建 Beautiful Soup 对象
soup BeautifulSoup(html)#打开本地 HTML 文件的方式来创建对象
#soup BeautifulSoup(open(index.html))#格式化输出 soup 对象的内容
print soup.prettify()输出结果
htmlheadtitleThe Dormouses story/title/headbodyp classtitle namedromousebThe Dormouses story/b/pp classstoryOnce upon a time there were three little sisters; and their names werea classsister hrefhttp://example.com/elsie idlink1!-- Elsie --/a,a classsister hrefhttp://example.com/lacie idlink2Lacie/aanda classsister hrefhttp://example.com/tillie idlink3Tillie/a;
and they lived at the bottom of a well./pp classstory.../p/body
/html运行后会看到这样一段警告
UserWarning: No parser was explicitly specified, so Im using the best available HTML parser for this system (lxml). This usually isnt a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.The code that caused this warning is on line 18 of the file D:/PycharmProject/spider/spider24_BeautifulSoup1.py. To get rid of this warning, pass the additional argument featureslxml to the BeautifulSoup constructor.soup BeautifulSoup(html)意思是如果我们没有显式地指定解析器所以默认使用这个系统的最佳可用HTML解析器(“lxml”)。如果你在另一个系统中运行这段代码或者在不同的虚拟环境中使用不同的解析器造成行为不同。
但是我们可以通过soup BeautifulSoup(html,“lxml”)方式指定lxml解析器
三. 四大对象种类
Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种:
TagNavigableStringBeautifulSoupComment
1. Tag
Tag 通俗点讲就是 HTML 中的一个个标签例如title head a p等等 HTML 标签加上里面包括的内容就是 Tag那么试着使用 Beautiful Soup 来获取 Tags:
from bs4 import BeautifulSouphtml
htmlheadtitleThe Dormouses story/title/head
body
p classtitle namedromousebThe Dormouses story/b/p
p classstoryOnce upon a time there were three little sisters; and their names were
a hrefhttp://example.com/elsie classsister idlink1!-- Elsie --/a,
a hrefhttp://example.com/lacie classsister idlink2Lacie/a and
a hrefhttp://example.com/tillie classsister idlink3Tillie/a;
and they lived at the bottom of a well./p
p classstory.../p
#创建 Beautiful Soup 对象
soup BeautifulSoup(html)print soup.title
# titleThe Dormouses story/titleprint soup.head
# headtitleThe Dormouses story/title/headprint soup.a
# a classsister hrefhttp://example.com/elsie idlink1!-- Elsie --/aprint soup.p
# p classtitle namedromousebThe Dormouses story/b/pprint type(soup.p)
# class bs4.element.Tag我们可以利用 soup 加标签名轻松地获取这些标签的内容这些对象的类型是bs4.element.Tag。但是注意它查找的是在所有内容中的第一个符合要求的标签。如果要查询所有的标签后面会进行介绍。
对于 Tag它有两个重要的属性是 name 和 attrs
print soup.name
# [document] #soup 对象本身比较特殊它的 name 即为 [document]print soup.head.name
# head #对于其他内部标签输出的值便为标签本身的名称print soup.p.attrs
# {class: [title], name: dromouse}
# 在这里我们把 p 标签的所有属性打印输出了出来得到的类型是一个字典。print soup.p[class] # soup.p.get(class)
# [title] #还可以利用get方法传入属性的名称二者是等价的soup.p[class] newClass
print soup.p # 可以对这些属性和内容等等进行修改
# p classnewClass namedromousebThe Dormouses story/b/pdel soup.p[class] # 还可以对这个属性进行删除
print soup.p
# p namedromousebThe Dormouses story/b/p2. NavigableString
既然我们已经得到了标签的内容那么问题来了我们要想获取标签内部的文字怎么办呢很简单用 .string 即可例如
print soup.p.string
# The Dormouses storyprint type(soup.p.string)
# In [13]: class bs4.element.NavigableString3. BeautifulSoup
BeautifulSoup 对象表示的是一个文档的内容。大部分时候,可以把它当作 Tag 对象是一个特殊的 Tag我们可以分别获取它的类型名称以及属性来感受一下
print type(soup.name)
# type unicodeprint soup.name
# [document]print soup.attrs # 文档本身的属性为空
# {}4. Comment
Comment 对象是一个特殊类型的 NavigableString 对象其输出的内容不包括注释符号。
print soup.a
# a classsister hrefhttp://example.com/elsie idlink1!-- Elsie --/aprint soup.a.string
# Elsie print type(soup.a.string)
# class bs4.element.Commenta 标签里的内容实际上是注释但是如果我们利用 .string 来输出它的内容时注释符号已经去掉了。
四. 遍历文档树
1.直接子节点 .contents .children 属性
1). .contents
tag 的 .content 属性可以将tag的子节点以列表的方式输出
print soup.head.contents
#[titleThe Dormouses story/title]输出方式为列表我们可以用列表索引来获取它的某一个元素
print soup.head.contents[0]
#titleThe Dormouses story/title2). .children
它返回的不是一个 list不过我们可以通过遍历获取所有子节点。
我们打印输出 .children 看一下可以发现它是一个 list 生成器对象
print soup.head.children
#listiterator object at 0x7f71457f5710for child in soup.body.children:print child结果:
p classtitle namedromousebThe Dormouses story/b/pp classstoryOnce upon a time there were three little sisters; and their names were
a classsister hrefhttp://example.com/elsie idlink1!-- Elsie --/a,
a classsister hrefhttp://example.com/lacie idlink2Lacie/a and
a classsister hrefhttp://example.com/tillie idlink3Tillie/a;
and they lived at the bottom of a well./pp classstory.../p2. 所有子孙节点: .descendants 属性
.contents 和 .children 属性仅包含tag的直接子节点.descendants 属性可以对所有tag的子孙节点进行递归循环和 children类似我们也需要遍历获取其中的内容。
for child in soup.descendants:print child运行结果
htmlheadtitleThe Dormouses story/title/head
body
p classtitle namedromousebThe Dormouses story/b/p
p classstoryOnce upon a time there were three little sisters; and their names were
a classsister hrefhttp://example.com/elsie idlink1!-- Elsie --/a,
a classsister hrefhttp://example.com/lacie idlink2Lacie/a and
a classsister hrefhttp://example.com/tillie idlink3Tillie/a;
and they lived at the bottom of a well./p
p classstory.../p
/body/html
headtitleThe Dormouses story/title/head
titleThe Dormouses story/title
The Dormouses storybody
p classtitle namedromousebThe Dormouses story/b/p
p classstoryOnce upon a time there were three little sisters; and their names were
a classsister hrefhttp://example.com/elsie idlink1!-- Elsie --/a,
a classsister hrefhttp://example.com/lacie idlink2Lacie/a and
a classsister hrefhttp://example.com/tillie idlink3Tillie/a;
and they lived at the bottom of a well./p
p classstory.../p
/bodyp classtitle namedromousebThe Dormouses story/b/p
bThe Dormouses story/b
The Dormouses storyp classstoryOnce upon a time there were three little sisters; and their names were
a classsister hrefhttp://example.com/elsie idlink1!-- Elsie --/a,
a classsister hrefhttp://example.com/lacie idlink2Lacie/a and
a classsister hrefhttp://example.com/tillie idlink3Tillie/a;
and they lived at the bottom of a well./p
Once upon a time there were three little sisters; and their names werea classsister hrefhttp://example.com/elsie idlink1!-- Elsie --/aElsie
,a classsister hrefhttp://example.com/lacie idlink2Lacie/a
Lacieanda classsister hrefhttp://example.com/tillie idlink3Tillie/a
Tillie
;
and they lived at the bottom of a well.p classstory.../p
...3. 节点内容: .string 属性
如果tag只有一个 NavigableString 类型子节点,那么这个tag可以使用 .string 得到子节点。如果一个tag仅有一个子节点,那么这个tag也可以使用 .string 方法,输出结果与当前唯一子节点的 .string 结果相同。
通俗点说就是如果一个标签里面没有标签了那么 .string 就会返回标签里面的内容。如果标签里面只有唯一的一个标签了那么 .string 也会返回最里面的内容。例如
print soup.head.string
#The Dormouses story
print soup.title.string
#The Dormouses story五. 搜索文档树
1. find_all(name, attrs, recursive, text, **kwargs)
1). name 参数
name 参数可以查找所有名字为 name 的tag,字符串对象会被自动忽略掉
A.传字符串 最简单的过滤器是字符串.在搜索方法中传入一个字符串参数,Beautiful Soup会查找与字符串完整匹配的内容,下面的例子用于查找文档中所有的 b标签:
soup.find_all(b)
# [bThe Dormouses story/b]print soup.find_all(a)
#[a classsister hrefhttp://example.com/elsie idlink1!-- Elsie --/a, a classsister hrefhttp://example.com/lacie idlink2Lacie/a, a classsister hrefhttp://example.com/tillie idlink3Tillie/a]B.传正则表达式 如果传入正则表达式作为参数,Beautiful Soup会通过正则表达式的 match() 来匹配内容.下面例子中找出所有以b开头的标签,这表示 body和 b标签都应该被找到
import re
for tag in soup.find_all(re.compile(^b)):print(tag.name)
# body
# bC.传列表 如果传入列表参数,Beautiful Soup会将与列表中任一元素匹配的内容返回.下面代码找到文档中所有 a标签和 b标签:
soup.find_all([a, b])
# [bThe Dormouses story/b,
# a classsister hrefhttp://example.com/elsie idlink1Elsie/a,
# a classsister hrefhttp://example.com/lacie idlink2Lacie/a,
# a classsister hrefhttp://example.com/tillie idlink3Tillie/a]2). keyword 参数
soup.find_all(idlink2)
# [a classsister hrefhttp://example.com/lacie idlink2Lacie/a]3).text 参数
soup.find_all(textElsie)
# [uElsie]soup.find_all(text[Tillie, Elsie, Lacie])
# [uElsie, uLacie, uTillie]soup.find_all(textre.compile(Dormouse))
[uThe Dormouses story, uThe Dormouses story]2. CSS选择器
这就是另一种与 find_all 方法有异曲同工之妙的查找方法.
写 CSS 时标签名不加任何修饰类名前加.id名前加#在这里我们也可以利用类似的方法来筛选元素用到的方法是 soup.select()返回类型是 list
1). 通过标签名查找
print soup.select(title)
#[titleThe Dormouses story/title]print soup.select(a)
#[a classsister hrefhttp://example.com/elsie idlink1!-- Elsie --/a, a classsister hrefhttp://example.com/lacie idlink2Lacie/a, a classsister hrefhttp://example.com/tillie idlink3Tillie/a]print soup.select(b)
#[bThe Dormouses story/b]2). 通过类名查找
print soup.select(.sister)
#[a classsister hrefhttp://example.com/elsie idlink1!-- Elsie --/a, a classsister hrefhttp://example.com/lacie idlink2Lacie/a, a classsister hrefhttp://example.com/tillie idlink3Tillie/a]3).通过 id 名查找
print soup.select(#link1)
#[a classsister hrefhttp://example.com/elsie idlink1!-- Elsie --/a]4).组合查找
组合查找即和写 class 文件时标签名与类名、id名进行的组合原理是一样的例如查找 p 标签中id 等于 link1的内容二者需要用空格分开
print soup.select(p #link1)
#[a classsister hrefhttp://example.com/elsie idlink1!-- Elsie --/a]直接子标签查找则使用 分隔
print soup.select(head title)
#[titleThe Dormouses story/title]5). 属性查找
查找时还可以加入属性元素属性需要用中括号括起来注意属性和标签属于同一节点所以中间不能加空格否则会无法匹配到。
print soup.select(a[classsister])
#[a classsister hrefhttp://example.com/elsie idlink1!-- Elsie --/a, a classsister hrefhttp://example.com/lacie idlink2Lacie/a, a classsister hrefhttp://example.com/tillie idlink3Tillie/a]print soup.select(a[hrefhttp://example.com/elsie])
#[a classsister hrefhttp://example.com/elsie idlink1!-- Elsie --/a]同样属性仍然可以与上述查找方式组合不在同一节点的空格隔开同一节点的不加空格
print soup.select(p a[hrefhttp://example.com/elsie])
#[a classsister hrefhttp://example.com/elsie idlink1!-- Elsie --/a]6). 获取内容
以上的 select 方法返回的结果都是列表形式可以遍历形式输出然后用 get_text() 方法来获取它的内容。
soup BeautifulSoup(html, lxml)
print type(soup.select(title))
print soup.select(title)[0].get_text()for title in soup.select(title):print title.get_text()