做微信公众平台的网站,如何访问win7下做的网站,抢先注册网站域名卖掉,化妆品网站建设实训总结BeautifulSoup 是一个可以从 HTML 或 XML 文件中提取数据的 Python 库#xff0c;它能够将 HTML 或 XML 转化为可定位的树形结构#xff0c;并提供了导航、查找、修改功能#xff0c;它会自动将输入文档转换为 Unicode 编码#xff0c;输出文档转换为 UTF-8 编码。
Beauti…BeautifulSoup 是一个可以从 HTML 或 XML 文件中提取数据的 Python 库它能够将 HTML 或 XML 转化为可定位的树形结构并提供了导航、查找、修改功能它会自动将输入文档转换为 Unicode 编码输出文档转换为 UTF-8 编码。
BeautifulSoup 支持 Python 标准库中的 HTML 解析器和一些第三方的解析器默认使用 Python 标准库中的 HTML 解析器默认解析器效率相对比较低如果需要解析的数据量比较大或比较频繁推荐使用更强、更快的 lxml 解析器。
1 安装
1BeautifulSoup 安装
如果使用 Debain 或 ubuntu 系统可以通过系统的软件包管理来安装apt-get install Python-bs4如果无法使用系统包管理安装可以使用 pip install beautifulsoup4 来安装。
2第三方解析器安装
如果需要使用第三方解释器 lxml 或 html5lib可是使用如下命令进行安装apt-get install Python-lxml(html5lib) 和 pip install lxml(html5lib)。
看一下主要解析器和它们的优缺点
解析器使用方法优势劣势Python标准库BeautifulSoup(markup,html.parser)Python的内置标准库执行速度适中文档容错能力强。Python 2.7.3 or 3.2.2)前的版本中文档容错能力差。lxml HTML 解析器BeautifulSoup(markup,lxml)速度快文档容错能力强。需要安装C语言库。lxml XML 解析器 BeautifulSoup(markup,[lxml-xml]) BeautifulSoup(markup,xml) 速度快唯一支持XML的解析器。需要安装C语言库html5libBeautifulSoup(markup,html5lib)最好的容错性以浏览器的方式解析文档生成HTML5格式的文档。速度慢不依赖外部扩展。
2 快速上手
将一段文档传入 BeautifulSoup 的构造方法就能得到一个文档的对象可以传入一段字符串或一个文件句柄示例如下
1使用字符串
我们以如下一段 HTML 字符串为例
html !DOCTYPE htmlhtml langenhead meta charsetUTF-8 titleBeautifulSoup学习/title/headbodyHello BeautifulSoup/body/html
使用示例如下 from bs4 import BeautifulSoup#使用默认解析器soup BeautifulSoup(html,html.parser)#使用 lxml 解析器soup BeautifulSoup(html,lxml)
2本地文件
还以上面那段 HTML 为例将上面 HTML 字符串放在 index.html 文件中使用示例如下
#使用默认解析器soup BeautifulSoup(open(index.html),html.parser)#使用 lxml 解析器soup BeautifulSoup(open(index.html),lxml)
2.1 对象的种类
BeautifulSoup 将 HTML 文档转换成一个树形结构每个节点都是 Python 对象所有对象可以归纳为4种TagNavigableStringBeautifulSoupComment。
1Tag 对象
Tag 对象与 HTML 或 XML 原生文档中的 tag 相同示例如下
soup BeautifulSoup(titleBeautifulSoup学习/title,lxml)tag soup.titletp type(tag)print(tag)print(tp)
#输出结果titleBeautifulSoup学习/titleclass bs4.element.Tag
Tag 有很多方法和属性这里先看一下它的的两种常用属性name 和 attributes。
我们可以通过 .name 来获取 tag 的名字示例如下
soup BeautifulSoup(titleBeautifulSoup学习/title,lxml)tag soup.titleprint(tag.name)
#输出结果#title
我们还可以修改 tag 的 name示例如下
tag.name title1print(tag)
#输出结果#title1BeautifulSoup学习/title1
一个 tag 可能有很多个属性先看一它的 class 属性其属性的操作方法与字典相同示例如下
soup BeautifulSoup(title classtlBeautifulSoup学习/title,lxml)tag soup.titlecls tag[class]print(cls)
#输出结果#[tl]
我们还可以使用 .attrs 来获取示例如下
ats tag.attrsprint(ats)
#输出结果#{class: [tl]}
tag 的属性可以被添加、修改和删除示例如下
#添加 id 属性tag[id] 1
#修改 class 属性tag[class] tl1
#删除 class 属性del tag[class]
2NavigableString 对象
NavigableString 类是用来包装 tag 中的字符串内容的使用 .string 来获取字符串内容示例如下 str tag.string
可以使用 replace_with() 方法将原有字符串内容替换成其它内容 示例如下 tag.string.replace_with(BeautifulSoup)
3BeautifulSoup 对象
BeautifulSoup 对象表示的是一个文档的全部内容它并不是真正的 HTML 或 XML 的 tag因此它没有 name 和 attribute 属性为方便查看它的 name 属性BeautifulSoup 对象包含了一个值为 [document] 的特殊属性 .name示例如下
soup BeautifulSoup(title classtlBeautifulSoup学习/title,lxml)print(soup.name)
#输出结果#[document]
4Comment 对象
Comment 对象是一个特殊类型的 NavigableString 对象它会使用特殊的格式输出看一下例子
soup BeautifulSoup(title classtlHello BeautifulSoup/title,html.parser)comment soup.title.prettify()print(comment)
#输出结果title classtl Hello BeautifulSoup/title
我们前面看的例子中 tag 中的字符串内容都不是注释内容现在将字符串内容换成注释内容我们来看一下效果
soup BeautifulSoup(title classtl!--Hello BeautifulSoup--/title,html.parser)str soup.title.stringprint(str)
#输出结果#Hello BeautifulSoup
通过结果我们发现注释符号 !---- 被自动去除了这一点我们要注意一下。
2.2 搜索文档树
BeautifulSoup 定义了很多搜索方法我们来具体看一下。
1find_all()
find_all() 方法搜索当前 tag 的所有 tag 子节点方法详细如下find_all(nameNone, attrs{}, recursiveTrue, textNone,limitNone, **kwargs)来具体看一下各个参数。
name 参数可以查找所有名字为 name 的 tag字符串对象会被自动忽略掉示例如下
soup BeautifulSoup(title classtlHello BeautifulSoup/title,html.parser)print(soup.find_all(title))
#输出结果#[title classtlHello BeautifulSoup/title]
attrs 参数定义一个字典参数来搜索包含特殊属性的 tag示例如下
soup BeautifulSoup(title classtlHello BeautifulSoup/title,html.parser)soup.find_all(attrs{class: tl})
调用 find_all() 方法时默认会检索当前 tag 的所有子孙节点通过设置参数 recursiveFalse可以只搜索 tag 的直接子节点示例如下
soup BeautifulSoup(htmlheadtitleHello BeautifulSoup/title/head/html,html.parser)print(soup.find_all(title,recursiveFalse))
#输出结果#[]
通过 text 参数可以搜搜文档中的字符串内容它接受字符串、正则表达式、列表、True示例如下
from bs4 import BeautifulSoupimport re
soup BeautifulSoup(headmyHead/headtitleBeautifulSoup/title,html.parser)#字符串soup.find_all(textBeautifulSoup)
#正则表达式soup.find_all(soup.find_all(textre.compile(title)))
#列表soup.find_all(soup.find_all(text[head,title]))
#Truesoup.find_all(textTrue)
limit 参数与 SQL 中的 limit 关键字类似用来限制搜索的数据示例如下
soup BeautifulSoup(a idlink1 hrefhttp://example.com/elsieElsie/aa idlink2 hrefhttp://example.com/elsieElsie/a,html.parser)soup.find_all(a, limit1)
我们经常见到 Python 中 *arg 和 **kwargs 这两种可变参数*arg 表示非键值对的可变数量的参数将参数打包为 tuple 传递给函数**kwargs 表示关键字参数参数是键值对形式的将参数打包为 dict 传递给函数。
使用多个指定名字的参数可以同时过滤 tag 的多个属性如
soup BeautifulSoup(a idlink1 hrefhttp://example.com/elsieElsie/aa idlink2 hrefhttp://example.com/elsieElsie/a,html.parser)soup.find_all(hrefre.compile(elsie),idlink1)
有些 tag 属性在搜索不能使用如 HTML5 中的 data-* 属性示例如下
soup BeautifulSoup(div data-foovaluefoo!/div)soup.find_all(data-foovalue)
首先当我在 Pycharm 中输入 data-foovalue 便提示语法错误了然后我不管提示直接执行提示 SyntaxError: keyword cant be an expression 这个结果也验证了 data-* 属性在搜索中不能使用。我们可以通过 find_all() 方法的 attrs 参数定义一个字典参数来搜索包含特殊属性的 tag示例如下 print(soup.find_all(attrs{data-foo: value}))
2find()
方法详细如下find(nameNone, attrs{}, recursiveTrue, textNone,**kwargs)我们可以看出除了少了 limit 参数其它参数与方法 find_all 一样不同之处在于find_all() 方法的返回结果是一个列表find() 方法返回的是第一个节点find_all() 方法没有找到目标是返回空列表find() 方法找不到目标时返回 None。来看个例子
soup BeautifulSoup(a idlink1 hrefhttp://example.com/elsieElsie/aa idlink2 hrefhttp://example.com/elsieElsie/a,html.parser)print(soup.find_all(a, limit1))print(soup.find(a))
#输出结果[a hrefhttp://example.com/elsie idlink1Elsie/a]a hrefhttp://example.com/elsie idlink1Elsie/a
从示例中我们也可以看出find() 方法返回的是找到的第一个节点。
3find_parents() 和 find_parent()
find_all() 和 find() 用来搜索当前节点的所有子节点find_parents() 和 find_parent() 则用来搜索当前节点的父辈节点。
4find_next_siblings() 和 find_next_sibling()
这两个方法通过 .next_siblings 属性对当前 tag 所有后面解析的兄弟 tag 节点进行迭代find_next_siblings() 方法返回所有符合条件的后面的兄弟节点find_next_sibling() 只返回符合条件的后面的第一个tag节点。
5find_previous_siblings() 和 find_previous_sibling()
这两个方法通过 .previous_siblings 属性对当前 tag 前面解析的兄弟 tag 节点进行迭代find_previous_siblings() 方法返回所有符合条件的前面的兄弟节点find_previous_sibling() 方法返回第一个符合条件的前面的兄弟节点。
6find_all_next() 和 find_next()
这两个方法通过 .next_elements 属性对当前 tag 之后的 tag 和字符串进行迭代find_all_next() 方法返回所有符合条件的节点find_next() 方法返回第一个符合条件的节点。
7find_all_previous() 和 find_previous()
这两个方法通过 .previous_elements 属性对当前节点前面的 tag 和字符串进行迭代find_all_previous() 方法返回所有符合条件的节点find_previous() 方法返回第一个符合条件的节点。
2.3 CSS选择器
BeautifulSoup 支持大部分的 CSS 选择器在 Tag 或 BeautifulSoup 对象的 .select() 方法中传入字符串参数即可使用 CSS 选择器的语法找到 tag返回类型为列表。示例如下
soup BeautifulSoup(bodya idlink1 classelsieElsie/aa idlink2 classelsieElsie/a/body,html.parser)print(soup.select(a))
#输出结果#[a clsselsie idlink1Elsie/a, a clsselsie idlink2Elsie/a]通过标签逐层查找 soup.select(body a)
找到某个 tag 标签下的直接子标签 soup.select(body a)
通过类名查找
soup.select(.elsie)soup.select([class~elsie])
通过 id 查找 soup.select(#link1)
使用多个选择器 soup.select(#link1,#link2)
通过属性查找 soup.select(a[class])
通过属性的值来查找 soup.select(a[classelsie])
查找元素的第一个 soup.select_one(.elsie)
查找兄弟节点标签
#查找所有soup.select(#link1 ~ .elsie)#查找第一个soup.select(#link1 .elsie)