2026/2/18 11:53:06
网站建设
项目流程
网站制作公司网站源码,手机网站的后台管理,wordpress静态首页设置,网站底部版权信息格式文章目录前言一、 安装1.1 BeautifulSoup 安装1.2 第三方解析器安装1.3 主要解析器对比二、快速上手2.1 使用字符串解析2.2 解析本地文件2.3 对象类型2.4 文档树搜索2.5 CSS 选择器前言
BeautifulSoup 是一个用于从 HTML 或 XML 文件中提取数据的 Python 库。它能够将文档转换…文章目录前言一、 安装1.1 BeautifulSoup 安装1.2 第三方解析器安装1.3 主要解析器对比二、快速上手2.1 使用字符串解析2.2 解析本地文件2.3 对象类型2.4 文档树搜索2.5 CSS 选择器前言BeautifulSoup 是一个用于从 HTML 或 XML 文件中提取数据的 Python 库。它能够将文档转换为可遍历的树形结构并提供导航、查找和修改功能。该库会自动将输入文档转换为 Unicode 编码输出时则转换为 UTF-8 编码。BeautifulSoup 支持 Python 标准库中的 HTML 解析器以及多种第三方解析器。默认情况下使用 Python 内置的 HTML 解析器但其解析效率相对较低。如果处理的数据量较大或解析频率较高建议使用性能更强的 lxml 解析器。一、 安装1.1 BeautifulSoup 安装Debian/Ubuntu 系统通过包管理器安装bashapt-getinstallpython-bs4通用安装方式使用 pipbashpipinstallbeautifulsoup41.2 第三方解析器安装如需使用 lxml 或 html5lib 解析器可按以下方式安装系统包管理器安装bashapt-getinstallpython-lxml python-html5libpip 安装bashpipinstalllxml html5lib1.3 主要解析器对比解析器使用方法优势劣势Python 标准库BeautifulSoup(markup, “html.parser”)Python 内置执行速度适中文档容错能力强Python 2.7.3 / 3.2.2 前版本容错能力较差lxml HTML 解析器BeautifulSoup(markup, “lxml”)速度快文档容错能力强需要安装 C 语言库lxml XML 解析器BeautifulSoup(markup, [“lxml-xml”]) 或 BeautifulSoup(markup, “xml”)速度快唯一支持 XML 的解析器需要安装 C 语言库html5libBeautifulSoup(markup, “html5lib”)最佳容错性以浏览器方式解析生成 HTML5 格式文档速度慢不依赖外部扩展二、快速上手将文档传入 BeautifulSoup 构造函数即可获得文档对象支持传入字符串或文件句柄。2.1 使用字符串解析示例 HTML 字符串python html !DOCTYPE html html langen head meta charsetUTF-8 titleBeautifulSoup学习/title /head body Hello BeautifulSoup /body /html 解析示例pythonfrombs4importBeautifulSoup# 使用默认解析器soupBeautifulSoup(html,html.parser)# 使用 lxml 解析器soupBeautifulSoup(html,lxml)2.2 解析本地文件假设上述 HTML 内容保存于 index.html 文件python# 使用默认解析器soupBeautifulSoup(open(index.html),html.parser)# 使用 lxml 解析器soupBeautifulSoup(open(index.html),lxml)2.3 对象类型BeautifulSoup 将文档转换为树形结构节点可分为四种类型Tag、NavigableString、BeautifulSoup、Comment。1Tag 对象Tag 对象对应 HTML/XML 中的原始标签python soupBeautifulSoup(titleBeautifulSoup学习/title,lxml)tagsoup.titleprint(tag)# titleBeautifulSoup学习/titleprint(type(tag))# class bs4.element.Tag常用属性name 属性获取或修改标签名pythonprint(tag.name)# titletag.nametitle1# 修改标签名print(tag)# title1BeautifulSoup学习/title1属性操作支持类字典方式访问python soupBeautifulSoup(title classtlBeautifulSoup学习/title,lxml)tagsoup.title# 获取属性print(tag[class])# [tl]print(tag.attrs)# {class: [tl]}# 增删改属性tag[id]1# 添加属性tag[class]tl1# 修改属性deltag[class]# 删除属性2NavigableString 对象包装标签中的文本内容python texttag.stringprint(text)# BeautifulSoup学习# 替换文本内容tag.string.replace_with(New Content)3BeautifulSoup 对象表示整个文档对象具有特殊属性python soupBeautifulSoup(title classtlBeautifulSoup学习/title,lxml)print(soup.name)# [document]4Comment 对象特殊类型的 NavigableString表示注释内容python soupBeautifulSoup(title classtl!--Hello BeautifulSoup--/title,html.parser)commentsoup.title.stringprint(comment)# Hello BeautifulSoup注释符号自动去除2.4 文档树搜索1find_all() 方法完整签名find_all(nameNone, attrs{}, recursiveTrue, textNone, limitNone, **kwargs)参数示例python# 按标签名查找print(soup.find_all(title))# [title classtlHello BeautifulSoup/title]# 按属性查找soup.find_all(attrs{class:tl})# 限制搜索深度soup.find_all(title,recursiveFalse)# 按文本内容查找importre soup.find_all(textBeautifulSoup)# 字符串soup.find_all(textre.compile(BeautifulSoup))# 正则表达式soup.find_all(text[head,title])# 列表soup.find_all(textTrue)# 所有文本节点# 限制结果数量soup.find_all(a,limit1)# 多条件过滤soup.find_all(hrefre.compile(elsie),idlink1)# 特殊属性搜索如>.find_all(attrs{data-foo:value})2find() 方法返回第一个匹配的节点无匹配时返回 Nonepython soupBeautifulSoup(a idlink1Elsie/aa idlink2Elsie/a,html.parser)print(soup.find_all(a,limit1))# 返回列表print(soup.find(a))# 返回单个节点3其他搜索方法方法功能描述find_parents() / find_parent()搜索父辈节点find_next_siblings() / find_next_sibling()搜索后续兄弟节点find_previous_siblings() / find_previous_sibling()搜索前序兄弟节点find_all_next() / find_next()搜索后续所有节点find_all_previous() / find_previous()搜索前序所有节点2.5 CSS 选择器BeautifulSoup 支持 CSS 选择器语法通过 .select() 或 .select_one() 方法调用python soupBeautifulSoup(bodya idlink1 classelsieElsie/a/body,html.parser)# 基本选择器soup.select(a)# 所有 a 标签soup.select(.elsie)# 按类名选择soup.select(#link1)# 按 ID 选择soup.select(a[class])# 有 class 属性的 a 标签soup.select(a[classelsie])# 属性精确匹配soup.select_one(.elsie)# 返回第一个匹配# 层级选择器soup.select(body a)# 后代选择器soup.select(body a)# 直接子元素soup.select(#link1 ~ .elsie)# 后续所有兄弟元素soup.select(#link1 .elsie)# 相邻兄弟元素# 多条件选择soup.select(#link1, #link2)# 多个选择器