2024-04-25 01:12:11 发布
网友
我试图通过删除所需的属性来抓取html页面。我可以删除带有空内容的标签,但却无法移除pilcrow标志
input: `<h2>Tutorial material<a>¶</a></h2>
预期产量:
代码:
elements = soup.find_all(True) for el in elements: if len(el.text) == 0: el.extract() print soup
这段代码删除带有空内容的标记,但我无法删除皮尔克劳符号
`
检查文本是否(仅)是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'¶'。在
u'¶'
您提供的代码删除了空节点,您只需修改它以包含@Robin注释。在
一种解决方案是检查节点文本是否为空或等于¶,然后将其删除:
elements = soup.find_all(True) for el in elements: if len(el.text) == 0 or el.text == u'¶': el.extract() print soup
检查文本是否(仅)是pilcrow:
尝试添加
在python文件的开头,并在需要时将pilcrow符号称为
u'¶'
。在您提供的代码删除了空节点,您只需修改它以包含@Robin注释。在
一种解决方案是检查节点文本是否为空或等于¶,然后将其删除:
相关问题 更多 >
编程相关推荐