- Beautiful Soup 教程
- Beautiful Soup - 首页
- Beautiful Soup - 概述
- Beautiful Soup - 网页抓取
- Beautiful Soup - 安装
- Beautiful Soup - 解析页面
- Beautiful Soup - 对象类型
- Beautiful Soup - 检查数据源
- Beautiful Soup - 抓取HTML内容
- Beautiful Soup - 通过标签导航
- Beautiful Soup - 通过ID查找元素
- Beautiful Soup - 通过类查找元素
- Beautiful Soup - 通过属性查找元素
- Beautiful Soup - 搜索树
- Beautiful Soup - 修改树
- Beautiful Soup - 解析文档的一部分
- Beautiful Soup - 查找元素的所有子元素
- Beautiful Soup - 使用CSS选择器查找元素
- Beautiful Soup - 查找所有注释
- Beautiful Soup - 从HTML中抓取列表
- Beautiful Soup - 从HTML中抓取段落
- BeautifulSoup - 从HTML中抓取链接
- Beautiful Soup - 获取所有HTML标签
- Beautiful Soup - 获取标签内的文本
- Beautiful Soup - 查找所有标题
- Beautiful Soup - 提取标题标签
- Beautiful Soup - 提取邮箱ID
- Beautiful Soup - 抓取嵌套标签
- Beautiful Soup - 解析表格
- Beautiful Soup - 选择第n个子元素
- Beautiful Soup - 通过标签内的文本搜索
- Beautiful Soup - 移除HTML标签
- Beautiful Soup - 移除所有样式
- Beautiful Soup - 移除所有脚本
- Beautiful Soup - 移除空标签
- Beautiful Soup - 移除子元素
- Beautiful Soup - find vs find_all
- Beautiful Soup - 指定解析器
- Beautiful Soup - 比较对象
- Beautiful Soup - 复制对象
- Beautiful Soup - 获取标签位置
- Beautiful Soup - 编码
- Beautiful Soup - 输出格式
- Beautiful Soup - 美化输出
- Beautiful Soup - NavigableString 类
- Beautiful Soup - 将对象转换为字符串
- Beautiful Soup - 将HTML转换为文本
- Beautiful Soup - 解析XML
- Beautiful Soup - 错误处理
- Beautiful Soup - 故障排除
- Beautiful Soup - 移植旧代码
- Beautiful Soup - 函数参考
- Beautiful Soup - contents 属性
- Beautiful Soup - children 属性
- Beautiful Soup - string 属性
- Beautiful Soup - strings 属性
- Beautiful Soup - stripped_strings 属性
- Beautiful Soup - descendants 属性
- Beautiful Soup - parent 属性
- Beautiful Soup - parents 属性
- Beautiful Soup - next_sibling 属性
- Beautiful Soup - previous_sibling 属性
- Beautiful Soup - next_siblings 属性
- Beautiful Soup - previous_siblings 属性
- Beautiful Soup - next_element 属性
- Beautiful Soup - previous_element 属性
- Beautiful Soup - next_elements 属性
- Beautiful Soup - previous_elements 属性
- Beautiful Soup - find 方法
- Beautiful Soup - find_all 方法
- Beautiful Soup - find_parents 方法
- Beautiful Soup - find_parent 方法
- Beautiful Soup - find_next_siblings 方法
- Beautiful Soup - find_next_sibling 方法
- Beautiful Soup - find_previous_siblings 方法
- Beautiful Soup - find_previous_sibling 方法
- Beautiful Soup - find_all_next 方法
- Beautiful Soup - find_next 方法
- Beautiful Soup - find_all_previous 方法
- Beautiful Soup - find_previous 方法
- Beautiful Soup - select 方法
- Beautiful Soup - append 方法
- Beautiful Soup - extend 方法
- Beautiful Soup - NavigableString 方法
- Beautiful Soup - new_tag 方法
- Beautiful Soup - insert 方法
- Beautiful Soup - insert_before 方法
- Beautiful Soup - insert_after 方法
- Beautiful Soup - clear 方法
- Beautiful Soup - extract 方法
- Beautiful Soup - decompose 方法
- Beautiful Soup - replace_with 方法
- Beautiful Soup - wrap 方法
- Beautiful Soup - unwrap 方法
- Beautiful Soup - smooth 方法
- Beautiful Soup - prettify 方法
- Beautiful Soup - encode 方法
- Beautiful Soup - decode 方法
- Beautiful Soup - get_text 方法
- Beautiful Soup - diagnose 方法
- Beautiful Soup 有用资源
- Beautiful Soup - 快速指南
- Beautiful Soup - 有用资源
- Beautiful Soup - 讨论
Beautiful Soup - diagnose() 方法
方法描述
Beautiful Soup 中的 diagnose() 方法是一个诊断套件,用于隔离常见问题。如果您难以理解 Beautiful Soup 对文档的操作,请将文档作为参数传递给 diagnose() 函数。报告将向您展示不同的解析器如何处理文档,并告诉您是否缺少解析器。
语法
diagnose(data)
参数
data − 文档字符串。
返回值
diagnose() 方法打印根据所有可用解析器解析给定文档的结果。
示例
让我们以这个简单的文档作为我们的练习 -
<h1>Hello World <b>Welcome</b> <P><b>Beautiful Soup</a> <i>Tutorial</i><p>
以下代码对上述 HTML 脚本运行诊断 -
markup = ''' <h1>Hello World <b>Welcome</b> <P><b>Beautiful Soup</a> <i>Tutorial</i><p> ''' from bs4.diagnose import diagnose diagnose(markup)
diagnose() 输出以显示所有可用解析器的消息开头 -
Diagnostic running on Beautiful Soup 4.12.2 Python version 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] Found lxml version 4.9.2.0 Found html5lib version 1.1
如果要诊断的文档是一个完美的 HTML 文档,则所有解析器的结果几乎相同。但是,在我们的示例中,有很多错误。
首先使用内置的 html.parser。报告如下所示 -
Trying to parse your markup with html.parser
Here's what html.parser did with the markup:
<h1>
Hello World
<b>
Welcome
</b>
<p>
<b>
Beautiful Soup
<i>
Tutorial
</i>
<p>
</p>
</b>
</p>
</h1>
您可以看到 Python 的内置解析器不会插入 <html> 和 <body> 标签。未关闭的 <h1> 标签在末尾提供匹配的 <h1>。
html5lib 和 lxml 解析器都通过将其包装在 <html>、<head> 和 <body> 标签中来完成文档。
Trying to parse your markup with html5lib
Here's what html5lib did with the markup:
<html>
<head>
</head>
<body>
<h1>
Hello World
<b>
Welcome
</b>
<p>
<b>
Beautiful Soup
<i>
Tutorial
</i>
</b>
</p>
<p>
<b>
</b>
</p>
</h1>
</body>
</html>
使用 lxml 解析器,请注意插入结束 </h1> 的位置。此外,不完整的 <b> 标签已得到纠正,并且已删除悬空的 </a>。
Trying to parse your markup with lxml
Here's what lxml did with the markup:
<html>
<body>
<h1>
Hello World
<b>
Welcome
</b>
</h1>
<p>
<b>
Beautiful Soup
<i>
Tutorial
</i>
</b>
</p>
<p>
</p>
</body>
</html>
diagnose() 方法也以 XML 文档的形式解析文档,这在我们的例子中可能多余。
Trying to parse your markup with lxml-xml
Here's what lxml-xml did with the markup:
<?xml version="1.0" encoding="utf-8"?>
<h1>
Hello World
<b>
Welcome
</b>
<P>
<b>
Beautiful Soup
</b>
<i>
Tutorial
</i>
<p/>
</P>
</h1>
让我们向 diagnose() 方法提供 XML 文档而不是 HTML 文档。
<?xml version="1.0" ?>
<books>
<book>
<title>Python</title>
<author>TutorialsPoint</author>
<price>400</price>
</book>
</books>
现在,如果我们运行诊断,即使它是 XML,也会应用 html 解析器。
Trying to parse your markup with html.parser
Warning (from warnings module):
File "C:\Users\mlath\OneDrive\Documents\Feb23 onwards\BeautifulSoup\Lib\site-packages\bs4\builder\__init__.py", line 545
warnings.warn(
XMLParsedAsHTMLWarning: It looks like you're parsing an XML document using an HTML parser. If this really is an HTML document (maybe it's XHTML?), you can ignore or filter this warning. If it's XML, you should know that using an XML parser will be more reliable. To parse this document as XML, make sure you have the lxml package installed, and pass the keyword argument `features="xml"` into the BeautifulSoup constructor.
使用 html.parser,会显示警告消息。使用 html5lib,包含 XML 版本信息的第 1 行被注释掉,其余文档被解析为 HTML 文档。
Trying to parse your markup with html5lib
Here's what html5lib did with the markup:
<!--?xml version="1.0" ?-->
<html>
<head>
</head>
<body>
<books>
<book>
<title>
Python
</title>
<author>
TutorialsPoint
</author>
<price>
400
</price>
</book>
</books>
</body>
</html>
lxml html 解析器不会插入注释,而是将其解析为 HTML。
Trying to parse your markup with lxml
Here's what lxml did with the markup:
<?xml version="1.0" ?>
<html>
<body>
<books>
<book>
<title>
Python
</title>
<author>
TutorialsPoint
</author>
<price>
400
</price>
</book>
</books>
</body>
</html>
lxml-xml 解析器将文档解析为 XML。
Trying to parse your markup with lxml-xml
Here's what lxml-xml did with the markup:
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" ?>
<books>
<book>
<title>
Python
</title>
<author>
TutorialsPoint
</author>
<price>
400
</price>
</book>
</books>
诊断报告可能有助于查找 HTML/XML 文档中的错误。