如何使用python(¶crow)在html之间签名

2024-04-25 01:12:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过删除所需的属性来抓取html页面。我可以删除带有空内容的标签,但却无法移除pilcrow标志

input: `<h2>Tutorial material<a>¶</a></h2>

预期产量:

^{pr2}$

代码:

elements = soup.find_all(True)
 for el in elements:
    if len(el.text) == 0:
        el.extract()
print soup

这段代码删除带有空内容的标记,但我无法删除皮尔克劳符号

`


Tags: 代码内容input属性标志html页面标签
3条回答

检查文本是否(仅)是pilcrow:

elements = soup.find_all(True)
for el in elements:
    if len(el.text) == 0 or el.text == u'¶':
        el.extract()
print(soup)

尝试添加

#!/usr/bin/env python
# -*- coding: utf-8 -*-

在python文件的开头,并在需要时将pilcrow符号称为u'¶'。在

您提供的代码删除了空节点,您只需修改它以包含@Robin注释。在

一种解决方案是检查节点文本是否为空或等于¶,然后将其删除:

elements = soup.find_all(True)
 for el in elements:
    if len(el.text) == 0 or el.text == u'¶':
        el.extract()
print soup

相关问题 更多 >