python - 从 html 中删除不匹配的结束标记
问题描述
我有一些 html 存储在一个字符串中。</span>
html 无效,并且在<td>
ie中包含不匹配的关闭
<table>
<tr><td>
<p>First section of text.</p>
<p>Second section of text.</span></p>
<table>
<tr><td>
<p>Third section of text.</p>
</td></tr>
</table>
</td></tr>
</table>
<p>Fourth section of text.</p>
当我将此html加载到BS中并使用以下方法提取为字符串时,我想使用BeautifulSoup来修改html:
soup = BeautifulSoup(html, 'html.parser')
print( str( soup.prettify() ) )
BS 大幅修改了结构。
<table>
<tr>
<td>
<p>
First section of text.
</p>
<p>
Second section of text.
</p>
</td>
</tr>
</table>
<table>
<tr>
<td>
<p>
Third section of text.
</p>
</td>
</tr>
</table>
<p>
Fourth section of text.
</p>
没有无与伦比的</span>
BS 输出,正如我所期望的那样
<table>
<tr>
<td>
<p>
First section of text.
</p>
<p>
Second section of text.
</p>
<table>
<tr>
<td>
<p>
Third section of text.
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
<p>
Fourth section of text.
</p>
我想做的是从 html 中删除不匹配的内容。如果不编写自己的解析器来寻找不匹配的标签,我怎么能做到这一点?我希望我可以使用 BS 来清理代码,但它不起作用。
解决方案
您可以将其拆分<span>
然后加入。
from bs4 import BeautifulSoup
data='''
<table>
<tr><td>
<p>First section of text.</p>
<p>Second section of text.</span></p>
<table>
<tr><td>
<p>Third section of text.</p>
</td></tr>
</table>
</td></tr>
</table>
<p>Fourth section of text.</p>
'''
soup=BeautifulSoup(data, 'html.parser')
data="".join(item.strip() for item in data.split("</span>"))
print(data)
这是打印的输出。
<table>
<tr><td>
<p>First section of text.</p>
<p>Second section of text.</p>
<table>
<tr><td>
<p>Third section of text.</p>
</td></tr>
</table>
</td></tr>
</table>
<p>Fourth section of text.</p>
<span>...</span>
如果您的 html 中存在标签并希望将其从 html 中删除,请使用分解。
from bs4 import BeautifulSoup
data='''
<table>
<tr><td>
<p>First section of text.</p>
<p>Second section of text.<span>xxxxx</span></p>
<table>
<tr><td>
<p>Third section of text.</p>
</td></tr>
</table>
</td></tr>
</table>
<p>Fourth section of text.</p>
'''
soup=BeautifulSoup(data, 'html.parser')
soup.span.decompose()
print(soup)
推荐阅读
- php - 提交表格帮助(IIS 网页)
- python - SQL炼金术 | 如何仅查询和过滤非 Null(或 None)的变量?
- laravel - 我如何将加入某事添加到我的 url (segemnt 1)
- amazon-web-services - 使用 AWS 云开发工具包 (CDK) 进行本地测试
- javascript - 使用 Object.keys Angular 获取 json 中的键和值
- c++ - 我怎样才能让我的蛇的身体扩大?
- python - 检查变量是 NaN 还是 Pandas 数据框中的 None
- java - IntelliJ HelloWorld Gradle 项目因 MultipleCompilationErrorsException 而失败
- c# - 刷新 CarouselView 项目后保持当前位置
- java - Java 证书、Eclipse 和 Maven:PKIX 路径构建失败