- 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 - 查找所有注释
在计算机代码中插入注释被认为是一种良好的编程实践。注释有助于理解程序的逻辑,也作为文档。你可以在HTML和XML脚本中添加注释,就像在用C、Java、Python等语言编写的程序中一样。BeautifulSoup API可以帮助识别HTML文档中的所有注释。
在HTML和XML中,注释文本写在<!-- 和 --> 标签之间。
<!-- Comment Text -->
BeautifulSoup包(内部名称为bs4)将Comment定义为一个重要的对象。Comment对象是一种特殊的NavigableString对象。因此,在<!-- 和 -->之间找到的任何Tag的string属性都被识别为Comment。
示例
from bs4 import BeautifulSoup markup = "<b><!--This is a comment text in HTML--></b>" soup = BeautifulSoup(markup, 'html.parser') comment = soup.b.string print (comment, type(comment))
输出
This is a comment text in HTML <class 'bs4.element.Comment'>
要搜索HTML文档中所有注释的出现,我们将使用find_all()方法。没有任何参数,find_all()返回解析的HTML文档中的所有元素。你可以将关键字参数'string'传递给find_all()方法。我们将把iscomment()函数的返回值赋给它。
comments = soup.find_all(string=iscomment)
iscomment()函数使用isinstance()函数验证标签中的文本是否是注释对象。
def iscomment(elem): return isinstance(elem, Comment)
comments变量将存储给定HTML文档中的所有注释文本。我们在示例代码中将使用以下index.html文件:
<html>
<head>
<!-- Title of document -->
<title>TutorialsPoint</title>
</head>
<body>
<!-- Page heading -->
<h2>Departmentwise Employees</h2>
<!-- top level list-->
<ul id="dept">
<li>Accounts</li>
<ul id='acc'>
<!-- first inner list -->
<li>Anand</li>
<li>Mahesh</li>
</ul>
<li>HR</li>
<ul id="HR">
<!-- second inner list -->
<li>Rani</li>
<li>Ankita</li>
</ul>
</ul>
</body>
</html>
以下Python程序抓取上述HTML文档,并查找其中的所有注释。
示例
from bs4 import BeautifulSoup, Comment
fp = open('index.html')
soup = BeautifulSoup(fp, 'html.parser')
def iscomment(elem):
return isinstance(elem, Comment)
comments = soup.find_all(string=iscomment)
print (comments)
输出
[' Title of document ', ' Page heading ', ' top level list', ' first inner list ', ' second inner list ']
上述输出显示所有注释的列表。我们也可以使用for循环遍历注释集合。
示例
i=0 for comment in comments: i+=1 print (i,".",comment)
输出
1 . Title of document 2 . Page heading 3 . top level list 4 . first inner list 5 . second inner list
在本章中,我们学习了如何提取HTML文档中的所有注释字符串。
广告